繁体   English   中英

使用.htaccess使用https将www和http重定向到非www

[英]Redirect www and http to non-www with https using .htaccess

我已将以下内容写入我的.htaccess文件,该文件位于index.html所在的目录中。

RewriteEngine On                              
RewriteCond %{HTTPS} off                        
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]                 
RewriteRule (.*) https://%1%{REQUEST_URL} [L,R=301]

I want that 
www.example.com       
http://example.com    
and http://www.example.com      

are redirected to 
https://example.com

我必须与example.com交换REQUEST_URI还是做其他错误的事情?

您必须在重写条件之间放置一个OR

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]

注意:您必须将网址测试为:

http://www.example.com

http://example.com

https://www.example.com

他们都将被重定向到

https://example.com

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM