.htaccess - Redirect old website to main domain but also some content to subdomain of the main domain

Viewed 70

I want to redirect an old domain (https & www) to the main one. I need to redirect it to the home page and blog. But, there are 4 pages on the old site I want to redirect to the subdomain of the main page.

How it should look:

Oldwebsite1.com -> mainwebsite2.com

Oldwebsite1.com/blog -> Mainwebsite2.com/blog

Oldwebsite1.com/category1 -> new.mainwebsite2.com/category1

Oldwebsite1.com/category2 -> new.mainwebsite2.com/category2 /// etc...

Will typical code like this work or do I need to adjust it?

#Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ http://www.mainwebsite2.com/$1 [R=301,L]
1 Answers

Based on your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs. These also handles optional www in your host name too.

RewriteEngine ON
##For Oldwebsite1.com -> mainwebsite2.com
RewriteCond %{HTTP_HOST} ^(?:www\.)Oldwebsite1\.com$ [NC]
RewriteRule ^/?$ http://mainwebsite2.com [R=301,L]

##For Oldwebsite1.com/blog -> Mainwebsite2.com/blog
RewriteCond %{HTTP_HOST} ^(?:www\.)Oldwebsite1\.com$ [NC]
RewriteRule ^(blog)/?$ http://mainwebsite2.com/$1 [NC,R=301,L]

##For Oldwebsite1.com/category1 -> new.mainwebsite2.com/category1 OR Oldwebsite1.com/category2 -> new.mainwebsite2.com/category2
RewriteCond %{HTTP_HOST} ^(?:www\.)Oldwebsite1\.com$ [NC]
RewriteRule ^(category1|category2)/?$ http://new.mainwebsite2.com/$1 [NC,R=301,L]
Related