sponsored

w3central.com

Apache

Subdomain on localhost

I create and test sites on my computer and prefer to use subdomains for that. Suppose you want to create a subdomain w3central.localhost, how would you do this?

You only have to change two things for this. First, you’ll need to find your Apache configuration file. This is called httpd.conf and resides somewhere in your apache directory. Open it and look for the virtual hosts section (mostly somewhere near the end). Normally you’ll find an example of the configuration of a virtual host inside it. Uncomment “NameVirtualHost *:80â€? and add a section like this:

<VirtualHost *:80>
ServerAdmin admin@localhost
DocumentRoot “path_to_your\htdocs\folder_you_wish_to_use”
ServerName w3central.localhost
</VirtualHost>

You can add more to the configuration, like a path to an error log (“ErrorLogâ€?), but you don’t strictly need this to have it up and running on your localhost.

Windows also wants to know about this. Browse to windows\system32\drivers\etc\ and open the file “hosts� with your text-editor. Add the line “127.0.0.1 w3central.localhost�.

Next and last step is to restart your Apache server and browse to w3central.localhost to test it. If you need more than one virtual host, you can simply add another “VirtualHost� section. The “NameVirtualhost *:80� is only needed once.