Save Time: Use Your Domain Instead of Localhost When Developing

One of the things I find annoying after developing in a local server is having to change the URLs from localhost to the actual domain name I will be using. Another annoyance about developing locally comes when you work with APIs because most of them require that you redirect to a real domain.

Apache virtual hosts solve these and more problems you might have because they let you redirect any domain name to any folder in your computer, this means that you can use your real domain for developing purposes without affecting what your visitors see.

Configuring Apache

The first file we need to mess with is httpd-vhosts.conf, this file is located in your apache folder in the following directory path.

apache\conf\extra\ httpd-vhosts.conf

You need to add the following lines to this file. The absolute path for DocumentRoot must be a folder located in your htdocs folder. Even if you are using windows you need to use &qout;/"s since this is what Apache uses.

<VirtualHost *:80>
   ServerAdmin admin@yourdomain.com
   DocumentRoot "absolute/path/to/your/local/folder"
   ServerName yourdomain.com
</VirtualHost>

You can make as many domains as you want, each domain will require it’s own pair of VirtualHost tags.

Configuring Your Computer

Now we need to tell your computer to redirect yourdomain.com to the folder DocumentRoot. We will do so by modifying the hosts file which is located in different places depending on your operating sytem.

For Windows:

C:\Windows\System32\drivers\etc\hosts

For Mac:

/private/etc/hosts

The following two lines should already be in the hosts file.

127.0.0.1       localhost
::1             localhost

Just add one line per domain now.

127.0.0.1       localhost
::1             localhost
127.0.0.1		yourdomain.com

That’s it! Make sure that you restart apache and close your browser any time you add or remove a domain.

Note:
Don’t forget to remove the virtual host any time you want to look at the actual contents of your server, otherwise you will looking at the content in your local server.