Mod rewrite subdomain htaccess with wildcard


When you use alot of subdomains on your website you can use a wildcard to catch all subdomains except www.

In this example we can rewrite http://mysubdomain.domain.com to http://www.domain.com/mysubdomain
RewriteEngine On
rewriteCond %{HTTP_HOST} !^(www\.)?domain\.com [NC] // if subdomain is not www
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com?$
RewriteRule ^$ http://www.domain.com/$1 [L]
We use two RewriteConds to first ensure we are not on the main domain and second to capture the subdomain into a backreference. The above example takes everything *.domain.com and rewrites it to a directory structure like www.domain.com/*.



Posted by James on 2009-07-30 in the category " apache "