Riding The Breeze

Jump to content

Additional information

Categories


Automatic vHost with Apache

May 2008
19

In my development machine i wanted to have a specific vHost for each project. Something that would allow me to type URL like:

I started to set for each one a vHost in Apache but after a while I've found myself with too many vHost files.

So I started to search for a more easy and fast way, and here it is:

Note: you need to install Apache mod_rewrite to make things work.

Preamble:

  1. You have Apache installed, and your DocumentRoot is /var/www/
  2. You have mod_rewrite installed and enabled
  3. You can modify the /etc/hosts file

Let's say we want to install a test/demo version of Wordpress and access it from the URL http://wp-test.lan/

  1. We download and untar Wordpress in /var/www/wp-test/
  2. We tell our machine to resolve the domain wp-test.lan with the IP 127.0.0.1. To make this we add a line to our hosts file.hpatoio@namazu:~$ sudo su -
    [sudo] password for hpatoio:
    root@namazu:~#echo "127.0.0.1 wp-test.lan" >> /etc/hosts
    root@namazu:~#exit
    hpatoio@namazu:~$
  3. Now we have to change Apache config. Open /etc/apache2/sites-anabled/000-default with your favorite editor and add these lines inside the <VirtualHost *> directive:RewriteEngine on
    RewriteCond %{HTTP_HOST} ^([^.]+)\.lan$
    RewriteRule ^(.+)$ /%1$1
    Basically :

    • The first line turns the mod_rewrite on
    • The second one tells Apache to keep in the host part of the URL (HTTP_HOST) everything that stands before ".lan"
    • Finally the third line insert the string matched before (%1) between the host and the resource part (directory + file + query string) of the requested URL
  4. Restart Apache
  5. hpatoio@namazu:~$sudo /etc/init.d/apache2 restart
    Now if open your browser and you point it to the URL http://wp-test.lan/ you should see the WP installation page. Right ?


Your Comment

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>




Credits

Template designed by praegnanz.de.