开发者

htaccess weird trailing slash problem

url: 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)

can anyone explain why there's a trailing slash for the second condition?


Also, how can I allow percentage开发者_如何学Python sign in url rewrite?

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

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

the above rule doesn't work. can anyone correct this so it allows a-Z, 0-9, dot, hyphen, and percentage sign?

Thanks!


You are getting the / for the second RewriteRule because .* is greedy. That is to say it greedily captures the trailing slash because you've marked it as optional /?. It's best to be specific with your patterns (like the first RewriteRule) to avoid such situations.

The pattern you match can accept anything. Just remember it has to be a valid URL. The problem is you forgot the quantifier. So you're only matching one character from the grouping.

Add the +

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜