Mod Rewrite - 需要Ungreedy修饰符吗?

时间:2012-01-30 14:04:41

标签: apache .htaccess mod-rewrite

我在.htaccess文件中有这段代码..

RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteRule ^(.+)-headquarters-([0-9]+)\.html company.php?lid=$2

现在我的问题是,如果第一个括号的匹配结果包含连字符会怎样?很多人都有很好的机会。这是ungreedy修饰符的来源吗??

1 个答案:

答案 0 :(得分:0)

我不认为在这种情况下你需要让它不合适。例如test-headquarters-headquarters-42.html完全匹配(test-headquarters$1)。

但为了完整起见,我会建议一些选项

您可以通过在其后面添加?来使其不合适。所以你的规则会变成(虽然,在这种情况下,不会出现导致不同行为的任何情况):

RewriteRule ^(.+)?-headquarters-([0-9]+)\.html company.php?lid=$2

您还可以使用

选择除连字符以外的任何内容
RewriteRule ^([^-]+)-headquarters-([0-9]+)\.html company.php?lid=$2
相关问题