Htaccess redirects for subdomains & # 8594; similarly named subdirectories?

I am reorganizing a website with a lot of content that is currently parked at URLs that look like this.

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

For various reasons, I would like to leave all new content at URLs that look like

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

I would like to make changes to future content, but I don't want all old things to go 404.

I believe that the 301 redirect rule in my htaccess will do the trick by sending all the traffic mentioned through the old links to the new formats.

But what should this rule look like? I read several manuals, but did not find this particular case in any examples.

Note that I do not want to do this for all subdomains, only for about 10 specific ones. Therefore, if someone can help me figure out one of them, then I can copy it 10 times to my htaccess for each subdomain and install it.

+4
source share
1 answer

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

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

This will take this part of the url on the old site:

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

and redirect it to a new site in a new place:

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

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

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

If you are redirected to the same domain and enable www , configure the rewrite rules as follows:

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

Pay attention to the second RewriteCond , which checks that the requested URL does not have a leading www , which can lead to endless redirection if the target URL itself includes www and would try to redirect this subdomain as well.

+4
source

All Articles