Setting up URL aliases for local development with WAMP

When developing a Drupal site that is hosted locally it's a good idea to setup a domain alias to access it by. 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. This is especially important when using the 'Domain Access' module, and to a lesser extent when utilising a Multi-site setup.

Personally I create URL aliases for all local developments as it is fairly straight forward when using something like WAMP or XAMPP. Below I describe how to do this for these two popular Windows web development platforms.

For the example lets assume that you have a locally hosted site in a folder called 'mySite' at:

  • C:/wamp/www/mySite   (WAMP)
  • C:/xampp/htdocs/mySite  (XAMPP)

With the above you would access your site at : http://localhost/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 like notepad. The file can be found at the below locations (path may differ slightly depending on Apache version).

  • C:\wamp\bin\apache\apache2.4.9\conf\extra    (WAMP)
  • C:\xampp\apache\conf\extra     (XAMPP)

2) 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 conf/extra/httpd-vhosts.conf

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.

  • C:\wamp\bin\apache\apache2.4.9\conf\extra    (WAMP)
  • C:\xampp\apache\conf\extra     (XAMPP)

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:

127.0.0.1  www.mysite.com

6) Restart your local server

Restart Apache on your WAMP or XAMPP installation, 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.

Justin Chevallier

Justin Chevallier

Avid Drupal site builder & user for +9 years.

Comments

Submitted by Derp on

Hello and thank you for such good tutorial! But i'm having problem in setting this alias for the whole internet.
My Drupal site is only acessable through my IP, and i want to make acessable for the alias as well... What should i do?
Thanks in advance!

Submitted by Justin on

If I understand you correctly you are going to need to purchase a domain name. This is basically like having an alias which is available to everyone on the internet without them needing to create a host entry which points to your IP. It's not possible to just create an alias as I've described and make that publically available.

Also from the sounds of things you are hosting this site on your local computer, so you should look at getting it hosted by an actual hosting provider. You can get both a domain name and basic hosting for quite a reasonable price these days.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.