Redirect Folder to Subdomain

When you purchase through links on our site, we may earn a commission. Here’s how it works.

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

Tagged With:

The information provided through this website should not be used to diagnose or treat a health problem or disease; it is not intended to offer any legal opinion or advice or a substitute for professional safety advice or professional care. Please consult your health care provider, attorney, or product manual for professional advice. Products and services reviewed are provided by third parties; we are not responsible in any way for them, nor do we guarantee their functionality, utility, safety, or reliability. Our content is for educational purposes only.

Notify of
Oldest Most voted
Inline Feedbacks
View all comments