【问题标题】:How to write RewriteCond and RewriteRule for changing url www to non-www and http to https?如何编写 RewriteCond 和 RewriteRule 以将 url www 更改为 non-www 并将 http 更改为 https?
【发布时间】:2016-06-27 19:53:10
【问题描述】:

在 Apache 的 httpd.conf 文件中,如何重写模块以使 url 的 www 为非 www 并强制 http 为 https? 当我在下面尝试时,它变成了无限重定向错误

RewriteCond %{HTTP_HOST} ^www.(.*)$
RewriteRule ^(.*)$ https://example.com$1

RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ https://example.com$1

RewriteCond %{HTTP_HOST} ^www.(.*)$ [OR]
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ https://example.com$1

【问题讨论】:

    标签: redirect mod-rewrite rewrite httpd.conf url-rewrite-module


    【解决方案1】:

    要从 www/http 重定向到非 www/https,您可以使用:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\. [OR]
    RewriteCond %{HTTPS} off 
    RewriteRule ^(.*)$ https://example.com%{REQUEST_URI [NC,L,R]
    

    【讨论】:

    • 我试过你的答案,它导致浏览器出现 ERR_TOO_MANY_REDIRECTS
    • @Jaylee 你确定我的代码出现了这个错误吗?实际上我在我的 apache 上测试了这个,它工作正常并且没有重定向循环错误的机会。
    猜你喜欢
    • 2014-12-16
    • 2017-12-04
    • 2023-03-16
    • 1970-01-01
    • 2018-12-30
    • 2015-06-18
    • 2018-07-05
    • 2016-10-14
    • 2011-09-15
    相关资源
    最近更新 更多