views:

1067

answers:

1

I'm restructuring a web site with a great deal of content currently parked at URLs that look like this.

http://string.domain.com/year/month/dd/string-pulled-from-title

For various reasons, I'd like to park all new content at URLs that looks like this

http://www.domain.com/blogs/string/year/month/dd/string-pulled-from-title

I'd like to make the change for future content, but don't want all the old stuff to go 404.

I believe a 301 redirect rule in my htaccess will do the trick, sending all referred traffic coming in through old links to the new formats.

But what should this rule look like? I've read a few tutorials but haven't found this exact case in any examples.

Note, I don't want to do this for all subdomains, only for about 10 specific ones. So if someone could help me figure out one of these, then I can copy paste it 10 times in my htaccess for each subdomain and be set.

+3  A: 

Drop this into the .htaccess file of the old site (adjusting the domain to your actual one):

RewriteEngine On
RewriteRule ^(.*)$ http://example.com/blogs/string/$1 [R=301]

This will grab this part of the URL at the old site:

year/month/dd/string-pulled-from-title

and redirect it to the new site under the new location:

blogs/string/year/month/dd/string-pulled-from-title

Alternatively, if you want something a little more variable like, without having to custom fix each .htaccess, drop this in the file for each subdomain instead:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteRule ^(.*)$ http://example.com/blogs/%1/$1 [R=301,L]

If you're redirecting to the same domain, and it includes the www, adjust the rewrite rules to the following:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteCond %{HTTP_HOST} !^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/blogs/%1/$1 [R=301,L]

Note the second RewriteCond which checks to make sure that the URL requested does not include the leading www, which may lead to an endless redirect if the destination URL itself includes www and would try and redirect that subdomain as well.

random
Not sure this is exactly what I was getting at, I need to do something a little more complex. Would this work?RewriteRule ^(.*)$ http://$1.example.com/$2 http://www.example.com/blogs/$1/$2 [R=301]That's the structure of what I need to do. But would that syndtax work?
bflora
Not really, but have taken that and applied up the edit to allow for a more flexible solution.
random
bflora
Ah, didn't account for the leading www, so have adjusted the answer in that case.
random
Awesome. That worked like a charm for fixing broken links to my old blog posts. Is there a way I can edit it so it only triggers for URLs that have stuff after the example.com? I'd like to redirect "subdomain.example.com" according to a different set of rules than "subdomain.example.com/something". This is the last missing piece. Thanks so much.
bflora
If you put `$` at the end of the domain in the `RewriteCond` that will stop it at that point in matching. Otherwise it will match to all that start the same, but may end differently.
random