Search code examples
apache.htaccesshttp-redirectmod-rewritehttp-status-code-301

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 ?


Solution

  • 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]