Article Image

If you're using a vps without a control panel for hosting websites, adding new websites to Apaches configuration might be a bit difficult.

Dynamic VirtualHosts to the rescue!

Using a simple VirtualHost command in Apache's httpd.conf, you can have Apache use dynamic virtualhosts, meaning that Apache serves a website using the requested url. With this, opening a website for a new domain is as easy as creating a directory.

The code for httpd.conf

<VirtualHost *:80>
    VirtualDocumentRoot /var/www/domains/%-2.0.%-1.0

Add this to the end of your httpd.conf.

How it works

This makes apache use /var/www/domains/ for all requests for or or any other subdomain. %-2.0 matches the second-to-last bit of the full domain, while %-1.0 matches the last, which means that the code ignores subdomains and serves the same directory for all of them. You can then canonize the domains with .htaccess redirects.

Now you can open unlimited domains just by creating directories under /var/www/domains/ !

Blog Logo

Juhani Naskali

The not-so-famous IT-specialist from Finland



Juhani Naskali

The not-so-famous IT-specialist from Finland.

Back to Overview