重写规则中的某些 url 除外



我在.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。

感谢您的任何建议...

首先,您需要确保将联系规则放在更通用的规则之前,以便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

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

最新更新