Creating virtual hosts in XAMPP

If you do not want to keep your files in the xampp/htdocs directory and type in a long URL for every page load, you can create virtual hosts for each of your sites instead. For example, you may like to keep all your sites in your My Documents folder for easy backup. Or you could create a root directory on your hard drive such as c:/sites/example/ which will make using shell commands easier. Wherever you choose to store your sites, you can always easily move them at a later date.

With XAMPP you set up virtual hosts in the following file:

\xampp\apache\conf\extra\httpd-vhost.conf

Open up that document in your text editor and append the following information to the bottom:

NameVirtualHost 127.0.0.1:80<VirtualHost 127.0.0.1:80>
DocumentRoot C:/xampp/htdocs/
ServerName localhost
</VirtualHost>

The above only needs to be added once and you don’t need to change any details to the above.

The next step, however, needs to be carried out for each site. Copy the code below and add it to the bottom of the file.

<VirtualHost example.local>
DocumentRoot "C:\path\to\your\site"
ServerName example.local
CustomLog “C:\path\to\your\site\logs\example.local.access.log” combined
ErrorLog “C:\path\to\your\site\logs\example.local.error.log”

<Directory “C:\path\to\your\site”>
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

You will need to makes some alterations for your personal setup.

Where it says example.local put the URL you would like to type into your browser to find your site. You can call this what you like, but make sure it relates to the site you are creating and is not a real URL that you may like to access on the Internet otherwise you will get a namespace clash. Using the production site’s URL but replacing .com or whatever with .local works well.

Where it says "C:\path\to\your\site" add the directory where your site is going to be stored on your local drive. Note that you should NOT include a slash on the end of the path. You can also set up a place to keep error and access logs for these sites. You will need to create the directory structure for these, but not the individual files (i.e. create the directory structure C:\path\to\your\site\logs but Apache will create the example.local.error.log file for you).

Finally you must include the domain names in your Windows host file. This file can be found at:

C:\WINDOWS\system32\drivers\etc\hosts

open this file in your text editor and add:

127.0.0.1 example.local

You can repeat this procedure for as many sites as you wish.

When you are done, restart Apache.

Clean URLs

You will also probably want to enable clean urls. To do this in XAMPP, open up xampp\apache\conf\httpd.conf and find this line:

#LoadModule rewrite_module modules/mod_rewrite.so

and uncomment it by deleting the hash (#) so that it reads:

LoadModule rewrite_module modules/mod_rewrite.so
Restart Apache and test that clean URLs work on your site. If this doesn’t work for some reason, follow the instructions on this handbook page

IF and only IF You should NOT have to take ownership of the file, do this:

Open an administrator command prompt (look above for the trick)
Type: cd \windows\system32\drivers\etc
Type: notepad hosts

Make your changes and save. I never had to take ownership of the file. It remains owned by SYSTEM like it should be.

1. Click Start>All Programs>Accessories>Notepad>Run as administrator.

2. Go to open, host location and open. Edit and save. Sorted.

More links on those types of resources, please share if you have more, thanks for posting:

Advertisements

One thought on “Creating virtual hosts in XAMPP

  1. equatorlounge says:

    the sad thing about all this is I can’t do it 😦 😦
    when i tried doing it, well it fucked up and my apache didnt start !
    had to abandon the idea, just like most innovative ideas i want to do, shiiiit

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s