问题
I'm using mod_rewrite for rewriting my links as follows. I defined a redirect from /test/1234_5678_...
to /test.php?id=1234
as follows:
RewriteRule test/(.*)_(.*)$ test.php?id=$1
It works perfectely. Now I wanted to add the following redirect: /test/1234_5678_.../print
to /test.php?id=1234&print
. Therefore I added the following line before the one above. The redirect is not working and it seems as if only the second rule applies. Am I doing anything wrong with the pattern matching? Is it a problem that there can be more than one underscore and I only used one in the pattern?
RewriteRule test/(.*)_(.*)/print$ test.php?id=$1&print
RewriteRule test/(.*)_(.*)$ test.php?id=$1
回答1:
Both rules work fine for me, but you probably want to change the first grouping to ([0-9]+)
or ([^_]+)
, and the second group to [^/]+
, and add some L
flags:
RewriteRule test/([^_]+)_([^/]+)/print$ test.php?id=$1&print [L]
RewriteRule test/([^_]+)_([^/]+)$ test.php?id=$1 [L]
来源:https://stackoverflow.com/questions/19625484/htaccess-rewriterule-pattern-matching