Using mod_rewrite with chinese characters in Apach

2019-07-16 13:00发布

问题:

I am having trouble finding information on Apache mod_rewriting using Chinese characters (all the info I can find relates to numbers).

I want to rewrite /character.php?character=宠 (where the character is the result of a search and thus will vary) to /character/宠.

This is my (poor) attempt: RewriteRule ^character/?$ characters?character=$1 [NC,L]

I would appreciate any help.

Thanks, Dan

回答1:

First of all, your Regular Expression is incorrect. Using the ? tells mod_rewrite that the character before it is optional. It is not a placeholder for any character.

You should be doing this instead:

RewriteRule ^character/(%[A-Z0-9]{3})$ characters?character=$1 [NC,L]

This rule assumes you want to only capture one character. If this is not the case, or you need the same rule elsewhere, then swap out (%[A-Z0-9]{3}) for (%[A-Z0-9]+).

You also need to make sure that your .htaccess file is saved in Unicode format (UTF-8).



回答2:

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule ^character/(.+)$ /characters?character=$1 [NE,NC,L,QSA]