简体   繁体   English

url中特殊字符的问题

[英]Problems with special characters in url

I am working on a client project and I am trying to improve the process of passing variables from a url to php. 我正在研究一个客户端项目,我正在尝试改进从url传递变量到php的过程。 The url structure of the project looks like the following: 该项目的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 Rules take this parameter "doesnotexist" and make it available in a $_GET variable in index.php. htaccess规则使用此参数“doesnotexist”并使其在index.php中的$ _GET变量中可用。

The variable gets encoded in javascript with encodeURIComponent, the url can be called in a browser and decoded in php with urldecode. 该变量使用encodeURIComponent在javascript中编码,可以在浏览器中调用url并使用urldecode在php中解码。 This works perfectly. 这非常有效。

Now to my problem: When the passed variable contains special chars like a slash "/" or an ampersand "&" it does not work anymore, because the browser thinks he is searching for a subdirectory. 现在我的问题:当传递的变量包含特殊字符如斜杠“/”或符号“&”时它不再起作用,因为浏览器认为他正在搜索子目录。 eg variable: "does/notexist" -> Browser tries to open http://xyz.com/does/notexist . 例如变量:“do / notexist” - >浏览器尝试打开http://xyz.com/does/notexist At the moment I'm replacing such characters like a slash with others that are no problems in a url before encoding. 目前我正在替换这些字符,比如在编码之前在网址中没有问题的其他字体。 So I replace "/" with "," or "&" with ";", encode it and everything is fine. 所以我将“/”替换为“,”或“&”替换为“;”,对其进行编码,一切都很好。 In my php script I decode it and replace "," with "/" and ";" 在我的PHP脚本中,我解码它并用“/”和“;”替换“,” with "&" and so one. 用“&”等等。 This works, but is really ugly, so I am searching for a better way to do it. 这有效,但真的很难看,所以我正在寻找一种更好的方法。

The initial url structure can not be changed. 初始url结构无法更改。 Does anyone know a better way to do this? 有谁知道更好的方法吗? I'm stuck here. 我被困在这里 One idea would be to base_encode the whole url parameter, but this is not the way I want it, because the url should be readable. 一个想法是base_encode整个url参数,但这不是我想要的方式,因为url应该是可读的。

Thid is a typical situation where you would use a .htaccess file. 这是您使用.htaccess文件的典型情况。 \\Use mod_rewrite. \\使用mod_rewrite。

from here: howto mod_rewrite every request to index.php except real files but exclude one real directory? 从这里: 如何mod_rewrite每个请求index.php除了真实文件,但排除一个真实的目录?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM