【问题标题】:Force SSL, WWW and remove index.php htaccess强制 SSL、WWW 并删除 index.php htaccess
【发布时间】:2014-06-19 10:41:33
【问题描述】:

如何使用 htaccess 实现所有这些。到目前为止,我有 -

强制实施 SSL

RewriteCond %{HTTPS} !on [OR]

RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]

RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301]

并删除 index.php

RewriteCond %{THE_REQUEST} ^.*/index.php

RewriteRule ^(.*)index.php$ https://www.example.com/$1 [R=301,L]

它似乎在大多数情况下都有效,但是当我输入类似“example.com/index.php”的内容时,我得到一个错误。它被重定向到

https://www.example.com/https://www.example.com/

我知道它为什么会发生,但无法找出一个规则来组合我需要的一切并使其发挥作用。

【问题讨论】:

    标签: .htaccess mod-rewrite ssl


    【解决方案1】:

    猜测一下,您在第一条规则中缺少L 标志:

    RewriteCond %{HTTPS} !on [OR]
    RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
    RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]
    

    如果没有 L 标志,请求会被标记为“重定向”,但会继续处理其他规则。根据您看到的结果,这两条规则似乎相互干扰。


    但是当我输入 example.com/index.php 时有什么方法可以减少重定向

    不确定这是否可行,因为我无法测试它,但也许是这样的?

    RewriteCond %{THE_REQUEST} ^.*/index.php [OR]
    RewriteCond %{HTTPS} !on [OR]
    RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
    RewriteRule ^(.*?)(index\.php)?$ https://www.example.com/$1 [R=301,L]
    

    【讨论】:

    • 嗨@jon。谢谢您的帮助。你说的对。但是当我输入 example.com/index.php 时,有什么方法可以减少重定向。现在它会先做两次重定向到 htts://www.example.com/index.php 然后到 htts://www.example.com/
    猜你喜欢
    • 1970-01-01
    • 2015-02-12
    • 2011-10-14
    • 2017-01-08
    • 1970-01-01
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    • 2012-08-15
    相关资源
    最近更新 更多