Localhost redirect

Hello everyone!

Okay tl;dr – I’m trying to setup a 301 redirect from http://localhost to http://dev.localhost, but when I tell that to netlify.toml, it seems to instead redirect me to /, which results in a redirect loop. Full story:

In order to get around an issue using magic.link with Brave, I need to access my dev site (which I start with netlify dev) at the domain http://dev.localhost instead of http://localhost. (Brave seems to resolve anything *.localhost to 127.0.0.1, which makes things easier for me, but if it didn’t then I’d just update /etc/hosts)

So nowadays I start up netlify dev, it automatically opens my browser to http://localhost, then I manually change that to http://dev.localhost in the address bar. And that does what I need it to do.

That works just fine, but I’d like to automate it. There doesn’t seem to be a way to tell netlify dev to open the page at a specific site (whereas, for example, snowpack allows the user to config a specific hostname to open … doesn’t help here, just wanted to show an analogous option in a different tool). Okay fine, maybe I can accomplish this with a redirect?

So I put in my netlify.toml file a section that looks like this:

[[redirects]]
force = true
from = "http://localhost"
status = 301
to = "http://dev.localhost"

But when I start the server, I just get an error about too many redirects. Looking in the network tab, I see that it’s redirecting me to / instead of http://dev.localhost. And indeed when I run curl I get

❯ curl http://localhost:8888
Redirecting to /%

I’ve also tried wildcarding the from and splatting the to, with the same results.

Is this normal/expected behavior for redirects? Is there some way I can configure a redirect to do what I want, and if not, is there some other way I can get the browser to automatically show me http://dev.localhost instead of http://localhost when I start up netlfiy dev?

hi there, before we dig in, did you see this brand new guide on debugging redirects?

I strongly suggest you give it a thorough read through and see if this fixes your problem:

if not, please post again, and we’ll try and troubleshoot.

Hi Perry,

Thanks for that info. Nothing there seemed helpful to this situation.

Here’s another detail I’m noticing – it does redirect, but it ignores the domain. In other words, if I do

from = "http://localhost"
to = "http://dev.localhost"

then it redirects to /, whereas if I do

from = "http://localhost"
to = "http://dev.localhost/asdf"

then it redirects to /asdf. In both of these examples, I’ve tried to tell it to go to the domain dev.localhost, which is the main thing I’m after, but it’s not doing it.

Please help.

I believe that the issue is because localhost isn’t attributed to the site/env. As localhost isn’t the domain for the site, we won’t apply the rule to it.

So in testing this last night, I had dev.mysite (and others) configured in apache. Even if I did

[[redirects]]
  from = "/dev"
  to = "http://dev.mysite"
  status = 301
  force = true

it would still to to / on localhost:8888.

Hi Pie,

If that’s the case, can you think of any other way to accomplish this?

Also, do you think the reasoning behind only applying redirect rules for domains listed in the site/env should also apply to localhost?

Hmmm… what about 127.0.0.1? Can’t imagine much difference but it’s worth a punt :smile:

It may also be worth taking a look at the CLI repo over on GitHub add running it past the team there. There are some other similar issues so the team may be interested in your findings too!

1 Like