.htaccess:重写规则模式匹配



我正在使用mod_rewrite重写我的链接,如下所示。我定义了一个从/test/1234_5678_.../test.php?id=1234的重定向,如下所示:

RewriteRule test/(.*)_(.*)$ test.php?id=$1

它完美地工作。现在我想添加以下重定向:/test/1234_5678_.../print/test.php?id=1234&print .因此,我在上面的行之前添加了以下行。重定向不起作用,似乎只有第二条规则适用。我在模式匹配方面做错了什么吗?是否可以有多个下划线,而我在模式中只使用了一个下划线?

RewriteRule test/(.*)_(.*)/print$ test.php?id=$1&print
RewriteRule test/(.*)_(.*)$ test.php?id=$1

这两个规则对我来说都很好用,但您可能希望将第一个分组更改为([0-9]+)([^_]+),将第二组更改为[^/]+,并添加一些L标志:

RewriteRule test/([^_]+)_([^/]+)/print$ test.php?id=$1&print [L]
RewriteRule test/([^_]+)_([^/]+)$ test.php?id=$1 [L]

最新更新