htaccess奇怪的尾随斜线问题

时间:2011-05-21 20:51:24

标签: .htaccess slash trailing

网址: http://localhost/url/of/the/keyword/whatever/

RewriteRule ^url/of/the/keyword/([a-z]+)/?$ ?keyword=$1 [L]  
// php  
echo $_GET['keyword'];    
// outputs **whatever** (OK)
RewriteRule ^url/of/the/keyword/(.*)/?$ ?keyword=$1 [L]  
// php  
echo $_GET['keyword'];  
// outputs **whatever/** (with a trailing slash, which is not expected)

任何人都可以解释为什么第二个条件有一个尾随斜线?


另外,如何允许百分号登录url重写?

http://localhost/url/of/the/keyword/%40%23%24/

RewriteRule ^url/of/the/keyword/([0-9a-zA-Z-.%])/?$ ?keyword=$1 [L]

以上规则不起作用。任何人都可以纠正这个,所以它允许a-Z,0-9,点,连字符和百分号?

谢谢!

1 个答案:

答案 0 :(得分:2)

您获得第二个/的{​​{1}}因为RewriteRule贪婪。也就是说,贪婪地捕获尾随斜杠,因为您已将其标记为可选.*。为了避免这种情况,最好特定一下你的模式(比如第一个/?)。

您匹配的模式可以接受任何内容。请记住它必须是有效的URL。问题是你忘了量词。所以你只匹配分组中的一个字符。

添加RewriteRule

+