我不知道为什么会失败。我只是想将所有域名重定向到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]
http://maindomain.com
至https:/www.maindomain.com/
正常工作http://anotherdomain.com
至https:/www.maindomain.com/
正常工作https://anotherdomain.com
至https:/www.maindomain.com/
失败答案 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重写进行重定向。