Yacasu
Yacasu

Reputation: 33

Redirect multiple domains (http https www & non-www) to a new domain using htaccess

I have 3 different domains (with two different TLD) and I would like that all my domains redirect to only one (including all format http https www or non-www) -> https://www.newdomain.com

All my domains :

http://olddomain.net
http://www.olddomain.net 
https://olddomain.net
https://www.olddomain.net 

http://olddomain2.com
http://www.olddomain2.com 
https://olddomain2.com
https://www.olddomain2.com

http://newdomain.com
http://www.newdomain.com 
https://newdomain.com
https://www.newdomain.com

So all these domains have to redirect to : https://www.newdomain.com

I have tried many different configurations of my htaccess but I do not handle all the redirections. So far what I found :

RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain\.net$ [OR]
RewriteCond %{HTTP_HOST} ^olddomain2\.com$ [OR]
RewriteCond %{SERVER_PORT} 80 [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.newdomain.com/$1 [L,NE,R=301]

But it does not handle all the variations. I think this is confusing Google bot as my new main url is not indexed.

How can I modify my htaccess so it handle all the possibilities ?

Upvotes: 2

Views: 82

Answers (2)

anubhava
anubhava

Reputation: 785226

It can be done using single redirect rule like this:

RewriteEngine On

RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.newdomain\.com$ [NC]
RewriteRule ^ https://www.newdomain.com%{REQUEST_URI} [NE,R=301,L]

Upvotes: 2

RavinderSingh13
RavinderSingh13

Reputation: 133528

Based on your shown samples, could you please try following. Please make sure you clear your browser cache before checking your URLs. I would have checked condition if your HTTP_HOST is NOT equal to your new domain but then I am afraid that it can touch your other domains too which you don't want to redirect to your new domain.

RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.net [NC]
RewriteRule ^ https://www.newdomain.com%{REQUEST_URI} [NE,R=301,L]

RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain2\.com [NC]
RewriteRule ^ https://www.newdomain.com%{REQUEST_URI} [NE,R=301,L]

Upvotes: 0

Related Questions