【问题标题】:Except some url from a rewrite rule除了重写规则中的一些 url
【发布时间】:2018-10-09 09:30:45
【问题描述】:

我在我的.htaccess 文件中插入了这条规则:

RewriteRule ^([A-Za-z0-9-]+)/?$   index.php?content=category&categoryname=$1  [NC,L]

通过这种方式,我可以获得这样的友好网址: http://localhost/mysite/london

我还想为我的联系页面使用一个友好的 URL,如下所示: https://localhost/mysite/index.php?content=message 成为: https://localhost/mysite/contact

但如果我将以下规则插入.htaccess...

RewriteRule   ^contact/?$   index.php?content=message  [NC,L] 

...它不起作用,因为类别的规则似乎会影响此规则。

其实如果我把分类规则注释掉的话……

#RewriteRule   ^([A-Za-z0-9-]+)/?$   index.php?content=category&categoryname=$1  [NC,L]

...联系页面的 url 友好规则有效 (https://localhost/mysite/contact)

所以我正在寻找从类别规则中排除某些参数的可能性,以便在某些情况下允许重定向到另一个 url。

感谢您的任何建议...

【问题讨论】:

    标签: php .htaccess url-rewriting


    【解决方案1】:

    首先,您需要确保将联系规则放在更通用的规则之前,以便 htaccess 可以首先处理它:

    RewriteRule ^contact/?$ index.php?content=message [NC,L]
    RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?content=category&categoryname=$1 [NC,L]
    

    如果您以其他方式绕过通用规则,则始终会在 htaccess 甚至到达联系规则之前查看和匹配。

    请注意,尽管您编写规则的方式只会匹配诸如

    之类的 URL

    https://exmaple.com/contact

    但不是

    https://exmaple.com/contact/something-else

    但是,看看你更通用的规则,我认为这是故意的。

    【讨论】:

    • 谢谢!是的,它只需要放在泛型之前
    猜你喜欢
    • 2020-04-11
    • 2012-10-30
    • 2012-05-03
    • 1970-01-01
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    相关资源
    最近更新 更多