Say you’re working on several projects at your local machine. Accessing those pages with custom URLs could be useful for a variety of reasons.
- Easier to remember the URL. http://abc is easier to remember than http://localhost/projects/2016/customer/abc
- If the project has a login form, your browser will cache the credentials for the domain, i.e. localhost. Working with custom URLs the browser will interpret them as different domains so it will cache the correct login. This is especially useful for me, I have like 5 local projects involving a login and I never know which one is correct for this and that.
So in the example below I assume you have Apache webserver running at http://localhost and a subfolder called “projects/mysite” (ok technically that’s 2 subfolders, but you get the picture)
Configure your hosts file
$ sudo nano /etc/hosts
Open Command Prompt in elevated mode and run
Add your custom URL at the end. For example:
Configure Apache to enable the virtual host module
httpd.conf which is somewhere in your apache folder, typically
vhost_ and find the line that says
LoadModule vhost_alias_module modules/mod_vhost_alias.so.
If this line is commented (has a
# at the beginning), then remove the
# to enable the module.
Set up a virtual host
httpd-vhosts.conf, typically found here:
You will probably see a list of commented entries here to illustrate how it works.
Now you must add a new virtual host. At the end of the file, paste in the following:
<VirtualHost *:80> ServerAdmin email@example.com DocumentRoot "C:/xampp/htdocs/projects/mysite" ServerName proj-mysite ServerAlias proj-mysite ErrorLog "logs/proj-mysite.log" CustomLog "logs/proj-mysite.log" common </VirtualHost>
I guess most of the lines are self-explonatory so edit wherever you need and save the file.
Restart Apache webserver
Then open http://proj-mysite