【问题标题】:Can't Force HTTPS and Non-WWW via htaccess无法通过 htaccess 强制 HTTPS 和非 WWW
【发布时间】:2018-05-04 02:19:49
【问题描述】:

我正在尝试强制任何常规 http 请求重定向到 https,并且还强制 URL 中的非 www,以便最终每个请求都是 https://example.com

http://example.com 变为 https://example.com

http://www.example.com 变为 https://example.com

https://www.example.com 变为 https://example.com

http://example.com 变为 https://example.com

我的 htaccess 文件中有以下代码。

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://example.com/$1 [R,L]

RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

但我收到一个错误,指出它有太多重定向。它似乎在这个测试站点上工作正常,输出正确:

htaaccess tester preview

对此有何想法或更好的方法?

【问题讨论】:

  • @anubhava 它显示 example.com 并说(失败)net::ERR_TOO_MANY_REDIRECTS
  • 使用命令行curl 运行这个命令curl -ILk 'http://example.com' 并发布它的输出。

标签: apache .htaccess redirect mod-rewrite


【解决方案1】:

以下代码应该对您有所帮助:

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,NE,R=301]

它将所有内容重定向到https://example.com

【讨论】:

    猜你喜欢
    • 2011-09-14
    • 2015-05-10
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 2019-09-08
    • 1970-01-01
    • 2013-05-31
    • 2011-06-17
    相关资源
    最近更新 更多