Categorized | Mod_Rewrite

Tags :

Rewrite subdomain to folder

Posted on 17 February 2010

You might have noticed some sites having their URLs written as if they were different subdomains of the same domain.

Internally rewrite .example.com/ to example.com/subs/<subdomain/

RewriteCond $1 !^subs/
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /subs/%1/$1 [L]

Internally rewrite .example.com/ to example.com/<subdomain/

RewriteCond %{ENV:Rewrite-Done} !^Yes$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /%1/$1 [E=Rewrite-Done:Yes,L]

In both cases, a mechanism is required (and present in the code) to prevent an ‘infinite’ rewriting loop.

If you use the first method, then you can easily externally redirect direct client requests for the subdomain-subdirectories back to the subdomain to avoid duplicate content:

Externally redirect client requests for example.com/subs// to .example.com/

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /subs/
RewriteRule ^subs/([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]

Here more complex email
The best way to do this is to setup each (or all) subdomain in the sites/{subdomain} directory of domain.com via your control panel. Some hosts will leave well enough alone (so the subdomain remains visible) but others actually force a redirect to the main domain’s subdirectory (forcing a change of URL). If you do this with mod_rewrite, you are forcing the change of URL.

# Rewrite <subdomain>.example.com/<path> to example.com/SITE/<subdomain>/<path>
#
# Skip rewrite if no hostname or if subdomain is www
# RewriteCond %{HTTP_HOST} .
# there must always be an {HTTP_HOST}
RewriteCond %{HTTP_HOST} !^www\. [NC]

# Extract (required) subdomain (%1), and first path element (%3), discard port number if present (%2)
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^([^.]+)\.domain\.com(:80)?<>/([^/]*) [NC]
# / is not used by Apache 2.x
# ([^/]*) means no subdirectories allowed

# Rewrite only when subdomain not equal to first path element (prevents mod_rewrite recursion)
RewriteCond %1<>%3 !^sites/(.*)<>\1$ [NC]
# Don't forget your sites "marker" subdirectory
# \1$ has no meaning!  All you don't want it sites/ in the URI

RewriteCond %{REQUEST_URI} !^/?sites/
# so you don't need the preceding RewriteCond statement

RewriteRule ^(.*) sites/%1/$1 [R=301,L]
# You might as well make this a 301 redirect
# finis!

    # Rewrite to /subdomain/path
    #RewriteRule ^(.*) /sites/%1/$1 [L]
    #RewriteRule ^(.*) /%1/$1 [L] #root directory
    #RewriteRule ^(.*) sites/%1/$1 [L]
    #RewriteRule ^/products/ index.php?id=$1&todo=

To check whether the folder exists is redundant – Apache will NOT even receive the request if the subdomain doesn’t exist so this problem can only occur with Dynamic Mass VirtualHosting. In that case, use a RewriteCond statement to check {PHYSICAL path to DocumentRoot}/sites/%1 -d (directory exists).

This post was written by:

- who has written 11 posts on The Developer.

PHP / MYSQL developer

Contact the author

Leave a Reply

You must be logged in to post a comment.