【问题标题】:.htaccess Edge Case.htaccess 边缘案例
【发布时间】:2010-10-28 15:41:55
【问题描述】:

我在 .htaccess 中使用这些规则和条件来转这些

http://www.example.com/index.php/about/history
http://www.example.com/about/history/index.php

进入

http://www.example.com/about/history

.htaccess

# ensure there is no /index.php in the address bar
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
    RewriteRule ^(.*)index\.php$ $1 [R=301,L,NS]

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php(.*)\ HTTP/ [NC]
    RewriteRule ^index\.php/(.*) $1 [NS,NC,L,R=301]

除非有一种情况,如果 URL 是

http://www.example.com/about/index.php/history/

这将导致无限循环的重定向。现在我知道这可能是一种罕见的情况,但如果可能的话,我希望它不要发生。如何更改我的规则以适应这种情况?

More info here about my .htaccess

【问题讨论】:

    标签: apache .htaccess url-rewriting friendly-url


    【解决方案1】:

    如果我理解正确,您只想在 index.php 出现的任何地方删除它?在这种情况下,您的解决方案过于复杂。你可能想要类似的东西

    RewriteRule ^(.*)index\.php/(.*)$ $1$2 [NS,NC,L,R=301]
    

    这应该是唯一的规则,也不需要 RewriteCond

    【讨论】:

    • 此规则在 URL 以 index.php/ 开头的情况下不起作用,例如第一个示例。但是,它确实适用于其他示例。
    • 将斜杠移到 index.php 字符串的后面。现在匹配了吗?
    • 注意。它现在在技术上也可以匹配 my_index.php 但我怀疑这不太可能发生
    猜你喜欢
    • 1970-01-01
    • 2016-03-30
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    • 2018-04-15
    • 2019-07-29
    相关资源
    最近更新 更多