Apache2使用ssl重定向到另一个域

时间:2016-02-02 10:42:22

标签: apache .htaccess redirect ssl

我不知道为什么会失败。我只是想将所有域名重定向到www.maindomain.com以及将http重定向到https,我缺少什么?

# 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]
  1. http://maindomain.comhttps:/www.maindomain.com/正常工作
  2. http://anotherdomain.comhttps:/www.maindomain.com/正常工作
  3. https://anotherdomain.comhttps:/www.maindomain.com/失败

2 个答案:

答案 0 :(得分:1)

Http to Https重定向另一个域失败,因为您的规则缺少以下行:

RewriteCond %{HTTPS} off

尝试:

# 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 :(得分:0)

你可以试试这个:

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

只需将上述代码复制并粘贴到.htaccess文件中,然后当浏览器以“http”模式打开时,整个网站将被重定向到“https”。浏览器只是在.htaccess中使用url重写进行重定向。