我可以将mod_rewrite与urlencode一起使用吗?

时间:2014-06-10 14:42:50

标签: php apache .htaccess mod-rewrite urlencode

我在PHP网络系统中使用mod_rewrite,我在.htaccess中有这个重写规则

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+).(pdf)$  /static/data/biblioteca/$1.$2 [L]

允许我写

<a href="http://my_domain.net/some_file.pdf" target="_new">title_of_some_file</a>

并在新的浏览器窗口中直接打开PDF文件,这样可以在看到源页面时伪装我的目录结构。

这样做很好,但有一件事。我住在巴西,葡萄牙语的标语符号为“á”,“é”,“ã”,“õ”,书中大部分标题都有这些符号。

当我使用urlencode获取正确的文件名时,似乎mod_rewirte感到困惑,因为它重定向文件但我收到错误404!

我知道我可能会把符号分开,用“a”代替“ã”等等,但我不想这样做,因为可能会出现一些尴尬的错误。例如,在葡萄牙语中,我们有“côco”意思是椰子,“cocô”意思是狗屎。如果我用“o”代替“ô”,那么两个单词的写法都是相同的,并且所有可能的(和恶心的)含义都会失真。

1 个答案:

答案 0 :(得分:2)

正如我在评论中所说,我刚刚测试过,它在我的Ubuntu / Linux操作系统上运行良好。所以看起来它是文本编码问题。

尝试使用[NE](禁止转义)标记

RewriteRule ^(.+).(pdf)$  /static/data/biblioteca/$1.$2 [L,NE]

如果这没有帮助,也许你可以考虑使用一些PHP

<强> htaccess的:

RewriteRule ^(.+).(pdf)$  /readpdf.php?name=$1 # (example)

PHP部分:

<?php

$filename = $_GET['name'];

// you may need to make sure there is no security flaws (like .. in name etc.)
// you may also need to convert encoding (from utf to iso for MS Windows)

$filepath = "static/data/biblioteca/" . $filename;
readfile($filepath . ".pdf");