This is guide on how to setup a domain aliases when doing local development using MAMP. By this I mean rather than accessing the site by something like http://localhost/my-site you set up an alias like http://test.my-site.com.
Personally I create URL aliases for all local developments as it is fairly straight forward and gets around the problem of having your site appear in a 'sub-folder'.
For the example lets assume that you have a locally hosted site in a folder called 'mySite' located in your home folder:
- ~/www (eg. /Users/joebloggs/www)
With the above you would access your site at something like: http://localhost:8888/mysite
To make it so that you can view your site at http://www.mysite.com follow these steps...
1) Edit 'httpd.conf' file
Locate and open this file using a text editor. The file can be found at the below location if MAMP is installed in the default Applications location.
Uncomment virtual hosts include path in 'httpd.conf' file
Search for the '# Virtual hosts' entry and uncomment the line 'Include conf/extra/httpd-vhosts.conf'. This will tell Apache to include the file where we will add our virtual host entries.
# Virtual hosts Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
Change the default port
Rather than using the MAMP default (8888) I like to set my port to 80. This way you don't have to reference your sites using something like http://localhost:8888, but can use just http://localhost. Change both of these entries in the httpd.conf from using 8888 to just 80.
Listen 80 ServerName localhost:80
3) Edit 'httpd-vhosts.conf' file
Locate and open this file using a text editor like notepad. This is where we will set the virtual host names.
4) Add a VirtualHost entry to your 'httpd-vhosts.conf' file
Paste this code into the bottom of the httpd-vhosts.conf file. Change the references to 'mySite' to match your settings.
Incidentally your server name doesn't have to be in the format www.mysite.com, you could use something like local.mysite which you would then visit in your browser at http://local.mysite (just make sure that the URL matches the one you add to your hosts file in the next step).
<VirtualHost *:80> # The name to respond to ServerName www.mysite.com # Folder where the files live DocumentRoot "c:/wamp/www/mySite" # A few helpful settings... <Directory "c:/wamp/www/mySite> allow from all order allow,deny # Enables .htaccess files for this site AllowOverride All </Directory> # Apache will look for these two files, in this order, if no file is specified in the URL DirectoryIndex index.html index.php </VirtualHost>
5) Edit your 'hosts' file
Again open this file using something like Notepad, and add an entry like this:
6) Restart your local server
Restart Apache on MAMP and refresh your browser - with some browsers you may need to close and re-open it. Now when you visit http://www.mysite.com you will be seeing your locally hosted site via it's domain alias.