.htaccess rewrite subdomain to directory

Erik Djupvik picture Erik Djupvik · May 17, 2012 · Viewed 191.9k times · Source

Is it possible to use .htaccess to rewrite a sub domain to a directory?

Example:

shows the content of

Answer

Ansari picture Ansari · May 17, 2012

Try putting this in your .htaccess file:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.domain.com
RewriteRule ^(.*)$ /subdomains/sub/$1 [L,NC,QSA]

For a more general rule (that works with any subdomain, not just sub) replace the last two lines with this:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ subdomains/%1/$1 [L,NC,QSA]