Snippets code from my daily experience

June 24, 2007

Simulating top level domains on localhost

Filed under: apache_httpd — dafi @ 8:44 am

On my local Apache test server I’ve configured many fake TLDs that take their contents from different directories (DocumentRoots).

But I found the problem to allow web browser to serve the correct content.

For example if I type the url http://dafizilla.stage I want to get content from /dafi/stage/dafizilla

if I type the url http://svn.myjavasrc I want to get content from /dafi/src/svn

Obviously dafizilla.stage and svn.myjavasrc are fake TLD and they are hosted on same Apache Server on my localhost.

Well the solution is to use Apache VirtualHost.

Add to /etc/hosts on Linux or C:/WINDOWS/…/etc/hosts on Windows the fake TLDs for localhost dafizilla.stage svn.myjavasrc

Then define in httpd.conf the VirtualHosts:

NameVirtualHost localhost

<VirtualHost localhost>
DocumentRoot /dafi/stage/dafizilla
ServerName dafizilla.stage
ErrorLog logs/stage-dafizilla-host-error_log

<VirtualHost localhost>
DocumentRoot /dafi/src/svn
ServerName svn.myjavasrc
ErrorLog logs/svn-host-error_log

The browser inserts into HTTP headers the domain name that is lookedup by Apache server

Blog at

%d bloggers like this: