【问题标题】:Problems with special characters in urlurl中特殊字符的问题
【发布时间】:2013-10-19 22:40:57
【问题描述】:

我正在开发一个客户端项目,我正在尝试改进将变量从 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 变量中可用。

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

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

初始的 url 结构不能改变。有谁知道更好的方法来做到这一点?我被困在这里。一种想法是对整个 url 参数进行 base_encode,但这不是我想要的方式,因为 url 应该是可读的。

【问题讨论】:

  • 你需要一个 .htacces 文件来做一些 url 重写,这会很容易

标签: php url


【解决方案1】:

这是您将使用 .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]

【讨论】:

    猜你喜欢
    • 2010-09-29
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    相关资源
    最近更新 更多