How to use a custom local dev url without having to edit the hosts file

Posted written by Paul Seal on August 15, 2017 Tips

So your are working on a website on your local dev machine and maybe you want to run the website in Local IIS with a custom url. You don't want to run it as an application under the default website, you want it to be a website on it's own with a custom url. So you need to get that custom url to point to your localhost, basically 127.0.0.1

Old Way

Here is how you would normally do it by adding the url to the host file:

  1. Open Notepad as an administrator by right-clicking on it in the start menu and choosing 'Run as administrator'
    Click File > Open > C:\Windows\System32\drivers\etc

  2. Above the open button where it says TextDocuments (*.txt) change it to All Files (*.*)

  3. Then select the file named hosts and click on Open.

  4. Add a new line at the bottom of the file like this:
    127.0.0.1 codeshare.local

  5. The number on the left is the IP address where you want that url to point to. Leave it as 127.0.0.1 so it points to your machine. the address on the right is the address of your site. Notice that it doesn't need http:// in front of if here.

  6. Save the file.

New Way

Here is how you can do it now.

  1. Just think of a url which ends with .localtest.me like this:
codeshare<strong>.localtest.me</strong>

How does it work?

Some kind person has set up the domain localtest.me and set the DNS for *.localtest.me to point to 127.0.0.1, so basically it just points to your local machine.

Go ahead and try it.

To test it out, you could use the command line to do the following:

ping anything.localtest.me

You will see it replies from 127.0.0.1. So there is no need to edit your hosts file any more for local development urls.

I was really pleased when I found this out from one of my YouTube subscribers, so I thought I should share it with you.

Please share this with others to let them know about it.

SECURITY NOTE:

Thanks to brunty and cemerson from the Tech Nottingham Slack Group for pointing out some security concerns with this. They were concerned that the domain owner could point the traffic to a malware site or something like that, which is a valid point, but I trust the person who registered the domain.

If you don't trust the person who set this domain up, you could always register a domain for yourself and point the subdomains to 127.0.0.1 to achieve the same thing.