2

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]
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
Zuzka
  • 21
  • 2

1 Answers1

3

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]
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • 2
    I think OP wants `new.mainwebsite2.com` for `category1|category2` – anubhava Jan 15 '21 at 14:55
  • 2
    @anubhava, Thank you sir I have changed it now, cheers. – RavinderSingh13 Jan 15 '21 at 18:52
  • Hello a thank you. I tried to use the code and it redirected well. But there is a problem. The blog redirected but blog posts are still on the old website. Is there a way to redirect them as well? – Zuzka Jan 18 '21 at 14:47
  • 1
    @Zuzka, could you please post sample url which is not working, let me know then. – RavinderSingh13 Jan 18 '21 at 14:55
  • Sure, I have blog with about 100 blog posts but only the blog category was redirected - without posts. So this is what doesnt work: "Oldwebsite.com/specific-blog-post-about-business/ - > Mainwebsite.com/specific-blog-post-about-business/ – Zuzka Jan 18 '21 at 15:04
  • 1
    @Zuzka, ok how about changing 8th line's rule to `RewriteRule (.*blog.*)/?$ http://mainwebsite2.com/$1 [NC,R=301,L]` once? Kindly do let me know how it goes. My current rule looks if uri starts from `block` but this rule will check if a uri contains `block` then perform redirection. – RavinderSingh13 Jan 18 '21 at 15:15
  • @RavinderSingh13 I changed it but blog posts are still on the old website. Everything else is working. – Zuzka Jan 18 '21 at 15:34
  • 1
    @Zuzka, how about changing 8th line with these 2 following Rules `RewriteCond %{REQUEST_URI} blog [NC] RewriteRule ^(.*)/?$ http://mainwebsite2.com/$1 [NC,R=301,L]` (these are 2 different lines showing single line in comments though)once and let me know. Please make sure you are changing your `^(?:www\.)Oldwebsite1\.com$ [NC]` on 7th line with your actual domain here too, cheers. – RavinderSingh13 Jan 18 '21 at 15:37
  • @RavinderSingh13 I changed the rules and nothing. It stopped redirecting "Oldwebsite1.com/blog" as well. – Zuzka Jan 18 '21 at 15:46
  • @Zuzka, ohh didn't think that should happen. Is string/word `blog` really existing inn your urls? Kindly confirm once, because its looking for blog string in uris. – RavinderSingh13 Jan 18 '21 at 15:48
  • @RavinderSingh13 I am using word `clanky` - but I always change word `blog` to `clanky` in the rules. The url exists as the main page for blog pages. – Zuzka Jan 18 '21 at 15:53
  • @RavinderSingh13 Sorry, I just noticed I used bad diacritics in `clanky` in the last rule. So I changed it. Blog category works fine but blog posts are still on the old website. – Zuzka Jan 18 '21 at 16:07
  • @Zuzka, could you please do let me know if you are using exactly same htaccess file which I shown or you have additional rules too? Kindly confirm once. – RavinderSingh13 Jan 18 '21 at 16:11
  • @RavinderSingh13 The one you recommended + These right under your rules: `RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] ` – Zuzka Jan 18 '21 at 16:15