Redirect Folder to Subdomain

To sustain this free service, we receive affiliate commissions via some of our links. This doesn’t affect rankings. Our review process.

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.

Disclaimer: This website contains reviews, opinions and information regarding products and services manufactured or provided by third parties. We are not responsible in any way for such products and services, and nothing contained here should be construed as a guarantee of the functionality, utility, safety or reliability of any product or services reviewed or discussed. Please follow the directions provided by the manufacturer or service provider when using any product or service reviewed or discussed on this website.

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.

5
Leave a Reply

avatar
newest oldest most voted
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)
Elvis Morales
Is it possible to achieve this without the “sd_” prepend?

Kimberly Alt (Admin)
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

takpar
Thanks for post. Perfect