Search code examples
regexapache.htaccesshttp-redirectmod-rewrite

Htaccess redirect https to another https


We have just bought the .com extension for our domain and we are using ssl certificates on both of them:

https://www.domain.net
https://www.domain.com

Now we would like to use the .com extension only and redirect our users from the old domain to the new one using the htaccess file:

http[s]://domain.net -------|
http[s]://www.domain.net ---|---> https://www.domain.com
http[s]://domain.com -------|

I am not very familiar with htaccess so I tried:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^http[s]?://(www\.)?domain\.com(.*)?$
RewriteRule ^(.*)$ http[s]?://www.domain.com/? [R,L]

That obviously doesn't work!

Any help would be greatly appreciated.


Solution

  • You cannot match scheme http:// in RewriteCond %{HTTP_HOST}.

    You can use this rule:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
    RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=302,L]