French characters with rewrite rule

2019-08-04 07:05发布

问题:

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]