I have added an entry to my hosts file which allows me to have localhost URLs like the following:


But I would like to somehow map this host entry to achieve the following instead:


Where 'index.php' lives at the path '~/Sites/mydomain/index.php'.

Is this possible? how to do this?


Editing MAC's host file

You will first need to edit your operating systems host file, on a mac this is a rather simple procedure that you should be able to follow with ease.

  1. Open your terminal application (normally /Applications/Utilities/Applications.app or you can may even perfer to use your standard text editor.
  2. In the terminal type nano /private/etc/hosts or open that file using your editor.
  3. Add example.com #Add a comment if you wish here to your hosts file, save it by doing control + o or save as within your editor.
  4. Now that your operating system hosts file has the nessacary changes you most likely need to flush your DNS by doing dscacheutil -flushcache

Adding your virtual host

You then need to instruct your HTTPD server that you have a domain that you want to attach to the server, this is referred as a virtual host. In this example we look at Apache and setting a example domain (there is dozens of questions and answers on virtual hosts on Pro Webmasters already.

You can add the virtual hosts by going into your enabled sites and editing the configation file and adding something like:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot "/Users/myusername/Sites/Test/public"
    <Directory "/Users/myusername/Sites/Test/public">
        Options Indexes FollowSymLinks Includes
        AllowOverride All
        Order allow,deny
        Allow from all

It's worth mentioning there are plently of tutorials on YourTube which will work you through setting up a LAMP.

  • Perfect, thanks! I wasn't aware of the 'VirtualHost' part. I found the following link helpful in completing the setup but your snippet above was ultimately what I needed :D coolestguidesontheplanet.com/… – Lea Hayes Nov 22 '14 at 21:29

It's possible, yes. Either you can:

  • change your httpd to redirect your URL. In apache that's with mod rewrite
  • Create a reverse proxy that redirects incoming URLs and rewrites.
  • Could you please explain how to achieve this using Terminal? Do I need to add something to /etc/apache2/httpd.conf? – Lea Hayes Nov 22 '14 at 21:07
  • Yes, you need to modify your httpd.conf and restart your server. – Sobrique Nov 22 '14 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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