问题
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