Roshan Book

My Tech Notebook

Want To Redirect A Folder To Your Subdomain?


This post was originally published at http://www.guruofsearch.com/redirect-folder-to-subdomain
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.)?example.com$
RewriteRule ^folder/(.*) http://subdomain.example.com/$1 [R=301,L]

This works fine if you’re redirecting a folder that is not associated with a subdomain (ie. the subdomain example.com/subdomain is stored, from the server’s point of view, at example.com/subdomain). 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. example.com/subdomain) to your subdomain URL (ie. subdomain.example.com), 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. subdomain.example.com), 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://$1.example.com/$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)>example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.([^.]+).example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^([^.]+).www.example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^([^.]+).example.com. [NC,OR]
RewriteCond %{HTTP_HOST} ^([^.]+).example.com.?:[0-9]+ [NC]
RewriteRule ^(.*)$ http://%1.example.com/$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} !^www.example.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+).example.com
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:

WordPress.com Logo

You are commenting using your WordPress.com 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: