.htaccess: RewriteRule pattern-matching

断了今生、忘了曾经 提交于 2019-12-13 19:51:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!