Using mod_rewrite with chinese characters in Apache

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 00:47:44

问题


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]


来源:https://stackoverflow.com/questions/16127162/using-mod-rewrite-with-chinese-characters-in-apache

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