.htaccess - 使用 SSL 将 www 重定向到非 www

标签 .htaccess redirect ssl

我看过很多关于重写的文章,但我有一个问题。

example.com => https://example.com(确定)
www.example.com => https://example.com(确定)
https://example.com => https://example.com (确定)
https://www.example.com => https://example.com(失败)

他试了几个论坛解决方案,但我有同样的问题。现在我的 htaccess 是:

<IfModule mod_rewrite.c>
RewriteEngine On

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

最佳答案

您可以使用:

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

在您的代码中,第二个测试没有使用 http 执行,%1 不正确。

关于.htaccess - 使用 SSL 将 www 重定向到非 www,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40822949/

相关文章:

.htaccess - 如何删除htaccess中的url参数

javascript - Coldfusion 中的重定向——还是强制 Javascript?

javascript - 将用户输入的数据附加到重定向 url

ssl - SSL 是否有任何内置的东西可以检测断开的连接?

ios - 如何使用 HTTPS 主机修复 IOS 应用程序中的 SSL 错误

apache - 请求/而不是/index.php时获取 "authentication required"

css - 重写CSS中字体的规则

.htaccess - htaccess 重定向除索引页之外的所有内容

c++ - 如何重定向在 C++ 中读取 bash 脚本的输出?

ssl - 受信任的根 CA 是否应该成为证书链的一部分?