Forum Moderators: phranque
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.
Regards
Watcher of the Skies
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.
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:
htt*://subdomain.mysite.com/
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!)
WOTS
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]
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.
Jim