url中特殊字符的问题

时间:2013-10-19 11:26:22

标签: php url

我正在研究客户端项目,我正在尝试改进将变量从url传递到php的过程。该项目的url结构如下所示:

http://xyz.com -> Domain
http://xyz.com/folder -> Folder/File
http://xyz.com/doesnotexist -> Folder/File does not exist
                            -> Pass it as a parameter to index.php Script

htaccess规则使用此参数“doesnotexist”并使其在index.php中的$ _GET变量中可用。

变量使用encodeURIComponent在javascript中编码,url可以在浏览器中调用,并使用urldecode在php中解码。这非常有效。

现在我的问题:当传递的变量包含斜杠“/”或&符号“&”等特殊字符时它不再起作用,因为浏览器认为他正在搜索子目录。例如变量:“does / notexist” - >浏览器尝试打开http://xyz.com/does/notexist。目前我正在替换这些字符,比如在编码之前在网址中没有问题的其他字体。所以我用“,”或“&”替换“/”用“;”编码,一切都很好。在我的PHP脚本中,我解码它并用“/”和“;”替换“,”用“&”等等。这很有效,但真的很难看,所以我正在寻找一种更好的方法。

初始网址结构无法更改。有谁知道更好的方法吗?我被困在这里一个想法是base_encode整个url参数,但这不是我想要的方式,因为url应该是可读的。

1 个答案:

答案 0 :(得分:2)

这是您使用.htaccess文件的典型情况。 \使用mod_rewrite。

从这里开始:howto mod_rewrite every request to index.php except real files but exclude one real directory?

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]