Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

301 Redirect with non-"real" Subdomains

They are actually subdirectories.....it's a cost thing.....


Watcher of the Skies

2:40 pm on Aug 30, 2005 (gmt 0)

10+ Year Member

With much help from many here at WebmasterWorld, I have determined that I must do a 301 redirect on my site. My site has ~100 subdomains. Also, these are "pesudo" subdomains I'm told, in that they merely point to subfolders and aren't "real" subdomains. Soooooooo, it seems like I need to redirect THREE possible duplicate instances of a page to the one I really want. And what I want is for all to point to the "non-www version" of a subdomain - not to a subfolder and not to a "www version". Thus...
1.) htt*://www.subdomain.mysite.com/
2.) htt*://www.mysite.com/subdomain/
3.) htt*://mysite.com/subdomain
...all need to be redirected to

I think I've figured out how to do this with the help of many, in particular member g1smd. This is how I understand to write the code:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.subdomain1.mysite.com [NC]
RewriteRule ^(.*)$ [subdomain1.mysite.com...] [L,R=301]
RewriteCond %{HTTP_HOST} ^www.mysite.com/subdomain1/ [NC]
RewriteRule ^(.*)$ [subdomain1.mysite.com...] [L,R=301]
RewriteCond %{HTTP_HOST} ^mysite.com/subdomain1/[NC]
RewriteRule ^(.*)$ [subdomain1.mysite.com...] [L,R=301]

My questions are:
1.) Is the code accurate?

2.) Should I copy the above syntax for each successive subdomain and place them all in the root's(http://www.mysite.com) .htacess file? Or do I need to place each redirect separately within the .htaccess file of each subDIRECTORY? Or is it something else altogether?

3.) This will, when done for 100 subdomains and if all placed in one .htaccess file, generate a lot of code. Is there a (practical) limit to how large an .htaccess file can be? Regardless of whether they're all in one file or not, will all these redirects PO the host?

Mind you, when alerted to the danger 48 hours ago, I had no idea what a 301 redirect was so please be nice.

Watcher of the Skies


5:25 pm on Aug 30, 2005 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

As long as they lead to content, they're "real" subdomains, all right. They're just not separately-hosted.

Let me ask a couple of clarifying questions:

Is it the case that you want to redirect *all* subdomains to your main domain, or just a few?

If more than just a few, but not all, are there more that you want to redirect than that you want to keep?

These questions with an eye to determining the most effective compact solution...

Now one comment to think about in the meantime: Best results will be had if you update many of your incoming links --from your own sites and from others-- so that they point to the 'right' domain; Using a redirect is not a cure-all, and is not the end of the project.


Watcher of the Skies

12:57 am on Aug 31, 2005 (gmt 0)

10+ Year Member

Hi Jim

Thanks for your response. For various reasons, I have always solicited external links, done internal links, and wished to show the sites in the format of:

I am only doing the redirect as I've found all of my listings (in Google) are starting to show up as URL's only, and after some reading, thought it a good idea to redirect those who search/enter/mistakently link to the "www version" of the site to the "non www version". In the course of doing this, I realized that as these subdomains were set up in the manner described, that it would probably be a good idea to redirect any attempted access through a subdirectory path, also. So, yes, I would like to redirect EVERY instance where either the reference contains www or where it contains a path in the format of a subdirectory.

Perhaps not the place for this, but you've prompted a question. When you say that "...they're "real" subdomains, all right." does that mean that for SE purposes they're treated most like
A.) a "real" subdomain
B.) a subdirectory
c.) different websites mimicking a virtual ip address type set-up (yikes!)



1:34 am on Aug 31, 2005 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

D. They are treated as URLs. Search engines don't care about server stuff, unless they have other reasons for suspicion. And then they don't care if it's a, b, or c anyway. Their main concern is the one you're trying to fix already -- massively-duplicate content.


Watcher of the Skies

6:46 am on Aug 31, 2005 (gmt 0)

10+ Year Member

Thanks again, Jim. Anyone have any ideas re my three questions at the top?


2:07 pm on Aug 31, 2005 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

I'd suggest the following changes to the code:

Options +FollowSymLinks
RewriteEngine on
# Remove "www." from any subdomain requests
# (Same rule will work for all subdomains)
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.mysite\.com [NC]
RewriteRule (.*) http://%1.mysite.com/$1 [R=301,L]
# Redirect client subdirectory requests to subdomain
# (Does not affect internal rewrites. One ruleset is required per subdomain)
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /subdomain1/
RewriteRule ^subdomain1/(.*) http://subdomain1.mysite.com/$1 [R=301,L]

I can't answer your question about whether you need to put copies of the code in each subdomain's subdirectory; The answer depends on your server configuration, and the method currently in use to rewrite subdomain requests to those subdirectories. However, as noted in the code, the first rule does not need to be modified, whether it is used in your top-level directory or duplicated in every subdomain's subdirectory.

The second ruleset is specifically constructed to avoid an infinite loop when it interacts which the mechanism currently used to rewrite subdomain requests to subdirectories. It is coded so that it only affects client requests for subdomain-subdirectories, and will not be invoked if the request for the subdirectory is generated by the server in response to a subdomain request.

The reason the second ruleset must be duplicated for each subdomain is that RewriteCond cannot test (compare) one variable against another, so the names of the subdomain-subdirectories must be hard-coded.

A practical maximum limit on .htaccess size might range from 4 kB to 100kB, depending on site traffic levels.



Featured Threads

Hot Threads This Week

Hot Threads This Month