user2643679
user2643679

Reputation: 705

Rewrite HTTPS domain to another HTTPS domain - htaccess

Currently I have

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://domain.com/$1 [R,L]

RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://domain.com/$1 [R,L]

RewriteCond %{HTTP_HOST} ^domain2\.com [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://domain.com/$1 [R,L]

RewriteCond %{HTTP_HOST} ^www\.domain2\.com [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://domain.com/$1 [R,L]

Which redirects www and non-www domain.com to https://domain.com And redirects www and non-www domain2.com to https://domain.com domain2 being a parked domain.

But you can still access https://domain2.com and https://www.domain2.com I want these redirected to https://domain.com Please advise, thanks

Upvotes: 0

Views: 3051

Answers (2)

Dusan Bajic
Dusan Bajic

Reputation: 10889

It should be as simple as this:

RewriteCond %{REQUEST_SCHEME} !https [NC,OR]
RewriteCond %{HTTP_HOST} !^domain.com$
RewriteRule ^ https://domain.com%{REQUEST_URI} [R=301,NE,L]

Still, keep in mind the last comment from @anubhava about configuring VirtualHost and SSL for domain2.com

Upvotes: 0

anubhava
anubhava

Reputation: 785531

You can combine some of these rules and modify redirect rule for domain2 -> domain to always redirect to https://domain.com:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(?:www\.)?(domain\.com)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,NE,L]

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

Upvotes: 1

Related Questions