French characters with rewrite rule

拜拜、爱过 提交于 2019-12-11 12:09:01

问题


I have categories name in my database, and some of theme have french characters like é è ê. It works as desired until today when I tried to add another character à to it then I started getting SERVER ERROR

Here is my .htaccess:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([A-Za-z0-9éèêà_-\s]+)-(\d+)\.htm$   classified.php?id=$2 [L]

As you can see, it works fine without the à.

How do I add that thing into the regex ?


回答1:


If you are using all sort of characters and accented letters, then why don't just allow anything to go by?

NOTE: Using this Rule will also allow spaces;

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)-(\d+)\.htm$   classified.php?id=$2 [L]

And later if you decide to limit the regex, lets say you don't want any of these characters # $ % then you have to make exceptions:

   Options +FollowSymlinks
  RewriteEngine on
  RewriteRule ^([^#$%]+)-(\d+)\.htm$   classified.php?id=$2 [L]



回答2:


Seems like extended ASCII characters in URL are sent as UTF-8 and url-encoded. For example:

/éèêà-1.htm -> /%C3%A9%C3%A8%C3%AA%C3%A0-1.htm

The above URL can be matched by mod_rewrite like this:

RewriteEngine On
RewriteRule ^(?:\w|\xC3\xA9|\xC3\xA8|\xC3\xAA|\xC3\xA0)+-(\d+)\.htm$ classified.php?id=$1 [L]


来源:https://stackoverflow.com/questions/14780875/french-characters-with-rewrite-rule

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