Mod_Rewrite Wildcard DNS/VHost Hack

I love to put client projects into their own sub-domain. It's clean for me AND the client. I hated having to wait for DNS/Apache changes to propagate, however.

Requirements

  • wildcard DNS
  • wildcard VHost
  • mod_rewrite

DNS

One of the things I've been meaning to put online is my mod_rewrite hack for wildcard DNS/VHosts. Once your DNS record is setup with a wildcard, any sub-domain gets directed to the same IP. You no longer have to define them. This is nice and all, but that's the easy part. Your Apache config still won't like it.

VHosts

Since most of us aren't allowed by our hosting company to modify our vhost config, we need another way. Some hosts will configure your VHost to direct ANY sub-domain to the same document root. If they don't already, ask them.

Mod_Rewrite

Great. Now ANY sub-main you enter will direct to the same document root. No DNS/VHost modifications required ever again. But, this doesn't allow us to have different content for each sub-domain. This is where mod_rewrite comes in. We can draft a .htaccess in our document root that will automatically pull the appropriate content for any sub-domain. The mod_rewrite commands are as follows... .htaccess

RewriteEngine on

# redirect if no sub-domain
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)$ [NC]
RewriteRule ^(.*)$ http://www.%1.%2/$1 [R,L]

# redirect domains
RewriteCond %{REQUEST_URI} !^/sites
RewriteCond %{HTTP_HOST} ^([^.]*)\.?([^.]+).([^.]+)$ [NC]
RewriteRule ^(.*)$ /sites/%3/%2/%1/$1

# fix trailing slash
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ $1/ [L]
First, the script redirects to the "www" sub-domain if none is given. This just makes things easier. The script will then look for content along a path with a specific format. The path is "sites/TLD/domain/sub-domain/" within the document root. For example, the content for "http://sub1.domain1.com" would be found in "sites/com/domain1/sub1/". The "sites" directory makes it easy to avoid recursive redirects. Finally, the script attempts to correct for a missing trailing slash.

Summary

You should now be able to just add a directory and have it appear as a new sub-domain. You may notice that the domain is not hard coded into the script. Yes, this script works on any level of the FQDN. You just have to setup all domains as described above.

Potential Problems

If you use PHP, you may have your include path set to your document root. This allows you to easily include files no matter what your directory depth. This doesn't automatically work anymore. The mod_rewrite script only rewrites URLs. The PHP include paths are handled separately. You can add some simple PHP to the beginning of your scripts to automatically set your NEW document root in your include path...

<?php
if ($_SERVER['HTTP_HOST'] != "")
{
   
$libpath = get_include_path() .
   
":/home/username/www/sites";
   
$parts = explode(".", $_SERVER['HTTP_HOST']);
    for (
$i=count($parts)-1; $i>=0; $i--)
       
$libpath .= "/" . $parts[$i];
   
set_include_path($libpath);
}
?>
If your host allows you to set the "auto_prepend_file" PHP directive in your .htaccess file, you can correct the include path without having to modify your all of your PHP scripts. Add the following to your .htaccess file...
# fix php include path based on [sub]domain
php_value auto_prepend_file /home/username/www/includedirfix.inc.php
Then insert the above PHP code into "/home/username/www/includedirfix.inc.php". The PHP code will automatically be prepended to every PHP script and you are good to go.

Trackback URL for this post:

http://www.techsanctuary.com/trackback/4

Beha's, de mooiste en grootste collectie bh's tegen de beste pri

Honderden beha's om uit te kiezen, alle bekende merken bh's voor de beste prijzen!

Baby annabell

Baby annabell