Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to map my directories/subdomains in the following way:

/home/sam/sites/* to http://*.sam

I don't want to have to add a line to my hosts file every time I spool up a new site.

I'm a beginner when it comes to configuring and installing apache, what's the best way to achieve this?

share|improve this question
Check out this apache doc: – mikewhatever Jul 11 '11 at 13:34
you mean http://localhost.sam or http://sam.localhost? – Achu Jul 11 '11 at 13:42
Mapping a subdomain for localhost requires editing the hosts file. – Nathan Osman Jul 11 '11 at 17:37
Sam152 - have you been able to achieve what you wanted following the comments above? – Mark Rooney Mar 17 '12 at 9:20
@Mark, I have listed the steps it took me to get it working below. – Sam152 Mar 17 '12 at 9:54
up vote 5 down vote accepted

It turns out there are quite a few steps involved in getting this working. Here is how I solved it:

  1. Edit /etc/resolve.conf to set your domain name resolution to your local machine by commenting out any existing lines and adding nameserver After this line add another line containing a secondary dns server for resolving other sites, eg nameserver Some program also overwrites this file every time you boot, so you can use chmod 0444 /etc/resolv.conf to prevent this from happening (there might be a better way to achieve this).
  2. Install dnsmasq, and at the end of /etc/dnsmasq.conf add a line address=/your-custom-local-domain/
  3. Create a new apache site config by placing a file called localdomain into /etc/apache2/sites-available with the text at the bottom of this answer.
  4. Run the following command sudo a2ensite config localdomain.
  5. Reboot apache, and create a new folder in ~/ called Sites.
<VirtualHost *:80>
    ServerAlias *.your-custom-local-domain
    VirtualDocumentRoot /home/%2/Sites/%1/
    #ErrorLog /home/%2/logs/error.%1.log

Now any new folder you create in the sites directory should be mapped to foldername.your-custom-local-domain.

share|improve this answer
Well done Sam!! – Mark Rooney Mar 17 '12 at 9:59
Thanks Mark, it took a while to figure out but was totally worth it for convenience sake. – Sam152 Mar 17 '12 at 10:07
This is ssssooooooooo much simpler than what we had been doing in the house. Thanks for sharing your solution! – James Mar 17 '12 at 10:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.