Apache2 redirects to another domain using ssl

I do not know why this fails. I just want to redirect the entire domain to www.maindomain.com , as well as http to https , what am I missing?

 # redirect http to https RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] # redirect without www to www RewriteCond %{http_host} ^maindomain.com [nc] RewriteRule ^(.*)$ https://www.maindomain.com [r=301,nc] # redirect another domain to www.maindomain.com RewriteCond %{HTTPS} off # this i was missing RewriteCond %{HTTP_HOST} ^(www\.)?anotherdomain.com [NC] RewriteRule ^(.*)$ https://www.maindomain.com [R=301,L] 
  • http://maindomain.com to https:/www.maindomain.com/ works
  • http://anotherdomain.com to https:/www.maindomain.com/ works
  • https://anotherdomain.com to https:/www.maindomain.com/ does not work
+5
source share
2 answers

The Http-Https redirection for another domain failed because the following line is missing from your rule:

 RewriteCond %{HTTPS} off 

Try:

 # redirect another domain to www.maindomain.com RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} ^(www\.)?anotherdomain.com [NC] RewriteRule ^(.*)$ https://www.maindomain.com [R=301,L] 
+1
source

You can try the following:

 RewriteEngine On RewriteCond %{HTTPS} !on RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} 

Just copy and paste the above code into the .htaccess file, then the whole website will be redirected to "https" when the browser opens in the "http" mode. The browser is simply redirected using url rewriting in .htaccess.

0
source

Source: https://habr.com/ru/post/1242083/


All Articles