Redirect Folder to Subdomain

How do we keep this site running? This post may contain affiliate links — the cost is the same to you, but we get a referral fee. Compensation does not affect rankings. Thanks!

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/(.*) //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_([^/]+)/(.*)$ //$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 ^(.*)$ //%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.

Looking to Redirect to a Virtual Subdirectory?

If you’e wanting to redirect all of a URL to a subdomain to a virtual subdirectory, check out this article.

Alex bring a series of in-depth articles on search marketing and content management systems as well as troubleshooting tips to We Rock Your Web's collection. He is an avid tennis player, nature enthusiast, and hiker, and enjoys spending time with his wife, friends, and dogs, Bella and Lily.

Leave a Reply

5 Comments on "Redirect Folder to Subdomain"

avatar
Karan Khanna
Karan Khanna

Thanks for the post. How can I redirect all url to a subdomain to a virtual subdirectory. Something like this – Redirect nz.domain.com/some-url to domain.com/nz/some-url where “some-url” can be anything.

Kimberly Alt
Admin
Kimberly Alt
takpar
takpar

Thanks for post. Perfect

Elvis Morales
Elvis Morales

Is it possible to achieve this without the “sd_” prepend?

Kimberly Alt
Admin
Kimberly Alt

Hi Elvis, thanks for reading and commenting! It is possible without the sd_ prepend, but you need to be careful and specify something unique in its place so the wrong folders don’t get redirected.

-Kimberly

wpDiscuz

Send this to a friend