Thursday, 5 August 2010

Dynamic subdomains on Apache and PHP

To rewrite subdomain urls internally to subdirectories under the root, ensure mod_rewrite is installed in Apache. Configure .htaccess inside the root directory as follows:-

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\
RewriteCond %{HTTP_HOST} ([^.]+)\
RewriteRule ^(.*)$ /home/sites/

Obviously replace “mydomain” and change the path on the last line to where your website physically resides on the server. Use the variable %1 to use the name of the subdomain as the name of the destination subdirectory.