Htaccess RewriteRule to accept special characters

断了今生、忘了曾经 提交于 2019-12-02 00:40:22

问题


My htaccess rewrite must handle these scenarios:

  • http://example.com/words/pantalón
  • http://example.com/words/pantal%C3%B3n
  • http://example.com/words/señor+señora

My current .htaccess configuration is:

RewriteRule ^dictionary/([\w\+]{2,50})$ /words.php?q=$1 [QSA,L]

It is not recognizing the special chars, e.g.: ñ, ó.

Any ideas? Thanks!


回答1:


Final solution

RewriteRule ^dictionary/([^/.]+)$ /words.php?q=$1 [QSA,L]



回答2:


Add a % to the character class in your expression:

RewriteRule ^dictionary/([\w+%]{2,50})$ /words.php?q=$1 [QSA,L]

Or you could even use [^/]{2,50}.

Special (something like [^A-Za-z0-9_]) characters are encoded by the client upon request. Note that åäö would become 9 characters, and even a single å would pass trough this expression. If you want to allow 50 special chars, use {2,150} and check both side of the range in your PHP code after decoding the string (which I guess is done for you automatically).




回答3:


Its better to convert the string into default url encode mechanism. AFAIK the request to url#1 cannot be made without encoding the url. Also the decoding would be done on the server side so you need not worry about that. If you are using some framework or server side script I would suggest to pass the pantalón via POST rather than get.

You can use jquery post http://api.jquery.com/jQuery.post/

If you still want to go through GET :

How to handle special characters in .htaccess rules?

Also be aware that the URLs will get encoded.



来源:https://stackoverflow.com/questions/8321602/htaccess-rewriterule-to-accept-special-characters

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