Dynamic virtualhost for Apache httpd.conf

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/domain.com for all requests for domain.com or www.domain.com 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/ !

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.