Roshan Book

My Tech Notebook

Want To Redirect A Folder To Your Subdomain?

This post was originally published at
Want To Redirect A Folder To Your Subdomain?

The straightforward method of using .htaccess to redirect a folder to a subdomain is as follows:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?$
RewriteRule ^folder/(.*)$1 [R=301,L]

This works fine if you’re redirecting a folder that is not associated with a subdomain (ie. the subdomain is stored, from the server’s point of view, at However, if you’re trying to redirect a subdomain folder to your subdomain using the above technique, you’ll end up in an endless loop.

Redirecting Subdomain Folder To Subdomain URL

It’s a good idea to redirect your subdomain’s folder (ie. to your subdomain URL (ie., because it will prevent people from accessing the folder version, and potentially seeing unwanted side effects (image paths not resolving, etc.).

While it’s possible to redirect your subdomain folder to your subdomain, so visitors will always end up at your subdomain address (ie., it’s quite complex, and involves an .htaccess method that will circumvent the looping problem described above by using a unique tag.

Here goes (credit goes to jdMorgan over at WMW for this example):

# Externally redirect direct client requests for subdomain-subdirectory URLs
# to subdomain URLs without subdomain-subdirectory URL-path
RewriteCond %{THE_REQUEST} ^[A-Z]+ /sd_[^/]+/
RewriteRule ^sd_([^/]+)/(.*)$ http://$$2 [R=301,L]
# Externally redirect non-canonical domains and subdomains
# (extra or missing “www”) to canonical domain and subdomains
RewriteCond www>%{HTTP_HOST} ^(www)> [NC,OR]
RewriteCond %{HTTP_HOST} ^www.([^.]+) [NC,OR]
RewriteCond %{HTTP_HOST} ^([^.]+) [NC,OR]
RewriteCond %{HTTP_HOST} ^([^.]+) [NC,OR]
RewriteCond %{HTTP_HOST} ^([^.]+)[0-9]+ [NC]
RewriteRule ^(.*)$$1 [R=301,L]
# Internally rewrite all but “main domain” URL-requests to
# subdomain subdirectory filepaths unless previously done
RewriteCond $1 !^sd_
RewriteCond %{HTTP_HOST} !^ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)
RewriteRule ^(.*)$ /sd_%1/$1 [L]

This will in effect redirect all subdomain folders with the “sd_” prefix to their respective subdomain destinations. This means that all subdomains that you wish to redirect will need to be appended with the “sd_” prefix. The example rules out “www.”

Duplicate Content Penalty

If you’re doing this example because you’re worried about a duplicate content penalty – our advice would be don’t. Search engines are smart enough now to discern subdomain addresses from their folders, and only index the former (if that weren’t the case, 99% of subdomains out there would be penalized, as it’s safe to assume that most don’t go to the trouble of implementing this approach).

Robots.Txt – Keep Search Engines Out Of The Subdomain Folder

However, if you’re concerned, and actually we recommend taking this approach regardless – add a “disallow: /subdomain” line in your root robots.txt file. What you won’t want to do is place a “disallow: /” (disallow all paths) in your subdomain folder. While it may look like this will prevent the folder from being accessed, since it’s treated as the root of your subdomain by search engines, it will effectively prevent your subdomain from being indexed.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: