Forum Moderators: phranque
What's the most efficient way of accomplishing this, via .htaccess or via mod_rewrite?
And how.
My current site has several subdomains, I simply want to change the design of my site permanently so that the sub domains become sub directories within the main directory, and have incoming traffic using old links directed from the old sub domain path to the new sub directory path.
Eg. [yellow.colours.com...]
is changed to:
[colours.com...]
I am aware that I can use lines like this in the root .htaccess file:
Redirectpermanent [yellow.colours.com...] [colours.com...]
BUT: the sub domain 'yellow' contains 100 files. That means 100 lines of code in my .htaccess file to be processed. Then another 100 in my sub domain, red, and another 100 in my sub domain blue etc. My .htaccess will be packed, and it'll slow down the site.
What I'm looking for is safe brief code that redirects a call for a file in a sub domain to the same file in sub directory without having to specify each file.
Along the lines of [yellow.colours.com...] [colours.com...]
If you get my drift.
Or using mod-rewrite if its safe and efficient.
Welcome to WebmasterWorld [webmasterworld.com]!
Sorry to keep you 'angin on fer so long... :)
Your question has been addressed in-depth here. One thread that may help is here: [webmasterworld.com...]
There are many others... Try a search of WebmasterWorld for "rewrite subdomain subdirectory" to see many more results. Perhaps some of the subtle variations discussed in these other threads will be useful.
Jim
In passing: I wonder what effect on Google ranking changing from a sub domain to a sub directory will have?
Will I lose my No.1 spot?
If the sub domain scores higher because it precedes a sub directory in the url, I could compensate by using a longer sub directory name.
Any thoughts?
Yes, search engines don't care about subdomains and subdirectories, they care about click-path lengths. How many clicks from page to page, in other words.
The only factor where URLs/pathnames come in is when you use keyword-in-URL tricks to get that last tenth of a percent of ranking. I prefer short, memorable URLs whenever possible, and usually don't bother with keyword-in-URL; Keyword-in-incoming-link-text is a much better way to spend your time, IMO.
If you redirect from an old URL to a new one using a 301-Moved Permanently redirect, the new URL will get the rank that the old one had, as long as you allow a month or two for the search engines to sort out the change. This is based on my experience, and your mileage may vary. Being naturally cautious, I would suggest you proceed slowly, and a step at a time if your livelihood depends on your site.
You should also make a concerted effort to get the other sites that link to your old URL to link to the new one instead. The 301 will help with those that don't update their links, but it's better not to rely on the 301 for 100% transfer of ranking.
Jim
I've read each of those threads, and several more pulled from a search here, but none of them remotely addresses the question I started this thread with. So I hope you'll be able to help me with this specific query.
From reading the threads though it does appear that using mod_rewrite would be a better option. But how?
Allow me to repose the same question with more explanation:
I'm switching to a new host to get away from the cPanel blues.
My site has several subdomains, but the new host charges anarmanaleg for setting up each one.
Ergo: I want to switch site from using sub domains to sub directories permanently.
Because each sub-dom has hundreds of pages inside it, if I use the .htaccess method: Redirectpermanent [yellow.colours.com...] [colours.com...] etc. etc ... I'll have an .htaccess a mile long.
So how do I briefly, efficiently and permanently redirect
Along the lines of [yellow.colours.com...] [colours.com...]
I'm eagerly waiting for your answer so that I can open the new account.
It's Apache (latest) with mod_rewrite btw.
PS. New Host's TOS state that using any method to circumvent charges (eg. for sub doms) is not allowed. So please understand, I'm *NOT* asking for a method to mimic sub doms, but legitimately redirect sub dom to sub dir of my restructured site. I know that this will incurr a loss of ranking in Google, but I'm hoping I can minimize this by naming the sub directory better than the sub domain, but this is of 2ry importance.
> I know that this will incurr a loss of ranking in Google.
No, it won't -- At least, it won't incur a permanent loss.
> [yellow.colours.com...] -> [colours.com...]
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.colours\.com [NC]
RewriteRule (.*) http://www.colours.com/%1/$1 [R=301,L]
RewriteEngine on enables rewriting.
The first RewriteCond will allow processing of the RewriteRule only if the subdomain is NOT "www." The [NC] flag makes the string comparison case-insensitive. This RewriteCond is required to prevent an "infinite loop" after the browser has been redirected to www.colours.com/<anysubdomain>/<anypage>
The second RewriteCond matches "colours.com" and extracts the subdomain into local variable "%1"
The RewriteRule extracts the requested page name into local variable "$1" and then plugs it into the substitution URL, along with the subdirectory name previously saved in "%1".
The [R=301] flag makes this an external 301-Moved Permanently redirect, which updates the URL in the user's browser address bar.
The [L] flag stops the RewriteEngine after this rewrite has taken place, so subsequent rewrite rules will not be processed *for this request*. (Since an external redirect is invoked, processing subsequent rules is very probably a complete waste of time.)
Please do not use the above code unless you understand it fully. One single typo can be disastrous, and I can't promise there isn't one. Again, I encourage you to study this powerful tool called mod_rewrite before trying to use it.
Ref: Introduction to mod_rewrite [webmasterworld.com]
Jim
I'm impressed.
Yes, understood; my responsibility. In fact I'm building a test rig on my current site to ensure it does what I want it to. I'll report back.
From your model; I take it I cannot rename my current subdomain to a new 'more keyword rich' sub directory, as I intended?
"The RewriteRule extracts the requested page name into local variable "$1" and then plugs it into the substitution URL, along with the subdirectory name previously saved in "%1"."
In that case, I'll use the exact same names, unless it's an easy thing to alter.
Thank you abundantly!
Colin
PS. Yes, I'm studying the docs and Dave's Primer :)
PSb. Just wondering ... Dave's Primer indicates a ^ should be closed off by a $ to indicate the pattern. So should there be a $ after the!^www\. ?
And another somewhere closing off the ^(.*)\.colours\.com ?
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST}!^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.colours\.com [NC]
RewriteRule (.*) [colours.com...] [R=301,L]
Must be the way cPanel sets up sub domains on my account. So when I try to create a sub directory with the same name, I get a 'Cannot Mate!' message.
I'll have to simply try the Redirection code at my new site once I get that set up.
> Just wondering ... Dave's Primer indicates a ^ should be closed off by a $ to indicate the pattern. So should there be a $ after the!^www\.?
No, that will break the code. I used ^ and $ VERY carefully in the code. A $ does not simply "close" a ^; they are independent operators and are used to "anchor" matching patterns. ^ is called a start anchor and $ is called an end anchor.
"^Something" means, "match only a string that starts with 'Something'." Similarly, "otherthing$" means "match only a string that ends with 'otherthing'." If both anchors are used, an exact match is required, and if neither is used, then any string which contains the given pattern in any position will be matched. See the regular-expressions primer link in Dave's mod_rewrite post cited above.
I'm not sure why you have a problem with the subdirectories -- the code was intended to work with subdirectories. The code can be placed in .htaccess in each existing subdomain/subdirectory and it will redirect to the main domain with the subdomain appended as a subdirectory.
You can change the subdirectory names at will. But you will likely have to update the cPanel settings. The code above does not care... It only "saves" the requested subdomain from one line of code to the next (as a local variable), and all is forgotten when the code exits.
Jim
As originally requested, this code redirects ALL files in subdomains to the same files in subdirectories:
Options +FollowSymLinks -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST}!^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.colours\.com [NC]
RewriteRule (.*) [colours.com...] [R=301,L]
How would we change this if we decided to *keep two* of our 4 subdomains on the new site, so we'd only need to redirect two of them to sub directories?
So we keep [red.colours.com...] and [blue.colours.com...]
But want to redirect:
[yellow.colours.com...] => [colours...] primrose.htm etc
[green.colours.com...] => [colours.com...] emerald.htm etc
Keep (do not redirect) subdomains red or blue (or www):
Options +FollowSymLinks -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www¦red¦blue)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.colours\.com [NC]
RewriteRule (.*) http://www.colours.com/%1/$1 [R=301,L]
I encourage you to experiment with this code, and to "learn to fish" for yourself, using the above code and the links cited above for examples. While this thread started out in general terms, it has (with my participation) become something we try to avoid [webmasterworld.com] here -- specifically a "write my code for me" thread. While we're happy to discuss general concepts, or to help you debug your own code, we try to avoid that. I hope this has helped, though.
Jim
I'm busy experimenting.
... CRASH! :)