用户
 找回密码
 入住 CI 中国社区
搜索
查看: 8619|回复: 18
收起左侧

包含某些中文字符的url会忽略.htaccess里的rewrite规则导致404

[复制链接]
发表于 2017-2-6 02:03:01 | 显示全部楼层 |阅读模式
最近出现了奇怪的问题   大部分中文地址都没有问题    但是某些中文文件会跳过rewrite规则直接访问从而出现404

经过我反复的验证发现   这些url出现问题的共同特征就是这个汉字被自动urlencode后带“%85”  

如“全”字编码后是%e5%85%a8   “入”字的编码是%e5%85%a5  “公”字的编码是%e5%85%ac

在config.php里把所有中文加入 $config['permitted_uri_chars'] 也解决不了问题
 楼主| 发表于 2017-2-6 02:06:01 | 显示全部楼层
经过查询  0085在unicode字码表里代表换行   不知道是不是这个导致的问题出现

无标题.png
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2017-2-10 15:50:52 | 显示全部楼层
Hex 发表于 2017-2-8 12:25
是的,你可以参考这个 http://httpd.apache.org/docs/current/mod/mod_rewrite.html 手册。

安全的“安”字:
[Fri Feb 10 15:31:25.720286 2017] [rewrite:trace3] [pid 2628:tid 1640] mod_rewrite.c(477): [client 192.168.1.9:51233] 192.168.1.9 - - [192.168.1.9/sid#6fac40][rid#260a0c8/initial] [perdir D:/xampp/htdocs/] add path info postfix: D:/xampp/htdocs/attachment -> D:/xampp/htdocs/attachment/download/\xe5\xae\x89
[Fri Feb 10 15:31:25.720286 2017] [rewrite:trace3] [pid 2628:tid 1640] mod_rewrite.c(477): [client 192.168.1.9:51233] 192.168.1.9 - - [192.168.1.9/sid#6fac40][rid#260a0c8/initial] [perdir D:/xampp/htdocs/] strip per-dir prefix: D:/xampp/htdocs/attachment/download/\xe5\xae\x89 -> attachment/download/\xe5\xae\x89
[Fri Feb 10 15:31:25.720286 2017] [rewrite:trace3] [pid 2628:tid 1640] mod_rewrite.c(477): [client 192.168.1.9:51233] 192.168.1.9 - - [192.168.1.9/sid#6fac40][rid#260a0c8/initial] [perdir D:/xampp/htdocs/] applying pattern '^(.*)$' to uri 'attachment/download/\xe5\xae\x89'
[Fri Feb 10 15:31:25.720286 2017] [rewrite:trace2] [pid 2628:tid 1640] mod_rewrite.c(477): [client 192.168.1.9:51233] 192.168.1.9 - - [192.168.1.9/sid#6fac40][rid#260a0c8/initial] [perdir D:/xampp/htdocs/] rewrite 'attachment/download/\xe5\xae\x89' -> 'index.php?/attachment/download/\xe5\xae\x89'
[Fri Feb 10 15:31:25.720286 2017] [rewrite:trace3] [pid 2628:tid 1640] mod_rewrite.c(477): [client 192.168.1.9:51233] 192.168.1.9 - - [192.168.1.9/sid#6fac40][rid#260a0c8/initial] split uri=index.php?/attachment/download/\xe5\xae\x89 -> uri=index.php, args=/attachment/download/\xe5\xae\x89
[Fri Feb 10 15:31:25.720286 2017] [rewrite:trace3] [pid 2628:tid 1640] mod_rewrite.c(477): [client 192.168.1.9:51233] 192.168.1.9 - - [192.168.1.9/sid#6fac40][rid#260a0c8/initial] [perdir D:/xampp/htdocs/] add per-dir prefix: index.php -> D:/xampp/htdocs/index.php
[Fri Feb 10 15:31:25.720286 2017] [rewrite:trace2] [pid 2628:tid 1640] mod_rewrite.c(477): [client 192.168.1.9:51233] 192.168.1.9 - - [192.168.1.9/sid#6fac40][rid#260a0c8/initial] [perdir D:/xampp/htdocs/] trying to replace prefix D:/xampp/htdocs/ with /
[Fri Feb 10 15:31:25.721286 2017] [rewrite:trace1] [pid 2628:tid 1640] mod_rewrite.c(477): [client 192.168.1.9:51233] 192.168.1.9 - - [192.168.1.9/sid#6fac40][rid#260a0c8/initial] [perdir D:/xampp/htdocs/] internal redirect with /index.php [INTERNAL REDIRECT]
[Fri Feb 10 15:31:25.721286 2017] [rewrite:trace3] [pid 2628:tid 1640] mod_rewrite.c(477): [client 192.168.1.9:51233] 192.168.1.9 - - [192.168.1.9/sid#6fac40][rid#9db11a0/initial/redir#1] [perdir D:/xampp/htdocs/] strip per-dir prefix: D:/xampp/htdocs/index.php -> index.php
[Fri Feb 10 15:31:25.721286 2017] [rewrite:trace3] [pid 2628:tid 1640] mod_rewrite.c(477): [client 192.168.1.9:51233] 192.168.1.9 - - [192.168.1.9/sid#6fac40][rid#9db11a0/initial/redir#1] [perdir D:/xampp/htdocs/] applying pattern '^(.*)$' to uri 'index.php'
[Fri Feb 10 15:31:25.721286 2017] [rewrite:trace1] [pid 2628:tid 1640] mod_rewrite.c(477): [client 192.168.1.9:51233] 192.168.1.9 - - [192.168.1.9/sid#6fac40][rid#9db11a0/initial/redir#1] [perdir D:/xampp/htdocs/] pass through D:/xampp/htdocs/index.php

安全的“全”字:
[Fri Feb 10 15:32:27.507820 2017] [rewrite:trace3] [pid 2628:tid 1652] mod_rewrite.c(477): [client 192.168.1.9:51245] 192.168.1.9 - - [192.168.1.9/sid#6fac40][rid#34350d8/initial] [perdir D:/xampp/htdocs/] add path info postfix: D:/xampp/htdocs/attachment -> D:/xampp/htdocs/attachment/download/\xe5\x85\xa8
[Fri Feb 10 15:32:27.507820 2017] [rewrite:trace3] [pid 2628:tid 1652] mod_rewrite.c(477): [client 192.168.1.9:51245] 192.168.1.9 - - [192.168.1.9/sid#6fac40][rid#34350d8/initial] [perdir D:/xampp/htdocs/] strip per-dir prefix: D:/xampp/htdocs/attachment/download/\xe5\x85\xa8 -> attachment/download/\xe5\x85\xa8
[Fri Feb 10 15:32:27.507820 2017] [rewrite:trace3] [pid 2628:tid 1652] mod_rewrite.c(477): [client 192.168.1.9:51245] 192.168.1.9 - - [192.168.1.9/sid#6fac40][rid#34350d8/initial] [perdir D:/xampp/htdocs/] applying pattern '^(.*)$' to uri 'attachment/download/\xe5\x85\xa8'
[Fri Feb 10 15:32:27.507820 2017] [rewrite:trace1] [pid 2628:tid 1652] mod_rewrite.c(477): [client 192.168.1.9:51245] 192.168.1.9 - - [192.168.1.9/sid#6fac40][rid#34350d8/initial] [perdir D:/xampp/htdocs/] pass through D:/xampp/htdocs/attachment



 楼主| 发表于 2017-2-7 16:40:20 | 显示全部楼层
本帖最后由 stalkerru 于 2017-2-7 16:50 编辑
Hex 发表于 2017-2-7 15:21
%85 这就是普通的 urlencode,一般来说一个中文的 utf-8 编码是 %e5 开头的三个字节,所以你的那个编码看 ...

服务器环境是Windows/Apache2.4    .htaccess的内容是这样的
  1. RewriteEngine onRewriteBase /
  2. RewriteCond $1 !^(favicon\.ico|index\.html|index\.php|static|robots\.txt)
  3. RewriteRule ^(.*)$ index.php?/$1 [L]
复制代码



比如 http://localhost/download/index/安生产规范 就可以正确转向到 http://localhost/index.php?/download/index/%e5%ae%89%e7%94%9f%e4%ba%a7%e8%a7%84%e8%8c%83

而 http://localhost/download/index/安生产规范 就会在apache里生成一条 http://localhost/download/index/%e5%ae%89%e5%85%a8%e7%94%9f%e4%ba%a7%e8%a7%84%e8%8c%83  404 not found的错误日志  就差一个字
发表于 2017-2-6 14:31:48 | 显示全部楼层
不要在 URI 中使用中文,把中文放到参数中就可以避免这个问题了。中文比较特殊,能不用就不要用。
 楼主| 发表于 2017-2-7 14:02:51 | 显示全部楼层
我现在很无奈地用base64对中文进行编码后传递了   但还是很想知道这是什么原因导致的
发表于 2017-2-7 15:21:31 | 显示全部楼层
stalkerru 发表于 2017-2-7 14:02
我现在很无奈地用base64对中文进行编码后传递了   但还是很想知道这是什么原因导致的
...

%85 这就是普通的 urlencode,一般来说一个中文的 utf-8 编码是 %e5 开头的三个字节,所以你的那个编码看起来是没问题的。

而 rewrite 这个是服务器的功能,跟 PHP 是没有关系的,所以要看 404 是谁抛出的 404,这个需要你提供更多信息来确认。
发表于 2017-2-7 17:19:54 | 显示全部楼层
stalkerru 发表于 2017-2-7 16:40
服务器环境是Windows/Apache2.4    .htaccess的内容是这样的

404 页面截图看一下。
 楼主| 发表于 2017-2-7 18:05:09 | 显示全部楼层
Hex 发表于 2017-2-7 17:19
404 页面截图看一下。




未标题-2.jpg
发表于 2017-2-7 19:42:24 | 显示全部楼层

你那里能打开 url rewrite log 吗?这个看起来是 rewrite 的问题,貌似是某些中文会不经过 rewrite 处理。
要解决问题,可以尝试用 urlencode() 处理中文以后再拼到 URL 中,不要用 base64
 楼主| 发表于 2017-2-7 21:10:21 | 显示全部楼层
Hex 发表于 2017-2-7 19:42
你那里能打开 url rewrite log 吗?这个看起来是 rewrite 的问题,貌似是某些中文会不经过 rewrite 处理 ...

url rewrite的log怎么打开?在apache的httpd.conf里吗

本版积分规则