繁体   English   中英

http://example.com、http://www.example.com和https://example.com到https://www.example.com

[英]http://example.com, http://www.example.com and https://example.com to https://www.example.com

关于堆栈溢出的第二个问题! 我们的目标是将对我们网站的所有请求永久重定向到https://www.example.com 目前,我们在下面的代码中将http://example.comhttp://www.example.com重定向到https://www.example.com ,但是,在花了很多时间在Stack Overflow上寻找之后解决方案中,我们还没有找到将https://example.com重定向到https://www.example.com 非常感谢您的帮助。

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

最好用一个规则来处理:

RewriteEngine On

# add www and force https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=302,L,NE]
  • 将此规则作为您的第一条规则。
  • 在测试之前,请确保清除浏览器缓存。
  • 测试完成后,将302更改为301

为了补充@anubhava的Apache优雅解决方案,我们最近找到了一种在Nginx上执行相同操作的方法:

server {
        listen 80;
        server_name example.com;
        return 301 https://www.example.com$request_uri;
}
server {
        listen 80;
        server_name www.example.com;
        return 301 https://www.example.com$request_uri;
}
server {
        listen 443;
        server_name example.com;
        return 301 https://www.example.com$request_uri;
}

暂无
暂无

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

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