Encoding special characters in url rewriting using htaccess

我是研究僧i 提交于 2019-12-11 11:48:48

问题


I have a rewrite rule as following,it is working:

RewriteRule area/(.*) listing.php?area=$1

But when I want to use %29 in it,but when I rewrite it as following, I get 404 error:

RewriteRule area/something%29/(.*)/ listing.php?area=$1 

Escaping %29 as \%29 also not works.


回答1:


Apache %-decodes the url-path before trying to apply the rewrite rules. So you should not use %-encoding in your RewriteRule. Just use the normal character.

So in your case you should just use the ). ) however is a special character in regular expression, so you should escape those in the RegEx way by adding a slash in front. So it will become \).

Your rule above should become:

RewriteRule area/something\)/(.*)/ listing.php?area=$1 



回答2:


The URL you want to rewrite is invalid as %29 would only be allowed in a Querystring, not a URL. You need to escape the % as %25 (and not with a backslash), so the resulting expression would be ...something%2529 - which should work.

For more in depth information check RFC2396.



来源:https://stackoverflow.com/questions/8822650/encoding-special-characters-in-url-rewriting-using-htaccess

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