【发布时间】:2017-12-15 05:33:46
【问题描述】:
所以我发现this interesting question 在使用重写规则后使问号符号出现在 $_GET 变量上。
但是,尽管我自己尝试过完成这项工作,但我不太明白如何在我的网站上获得相同的结果。
这是我的重写规则:
RewriteRule ^(.+)$ index.php?uri=$1 [QSA,L]
这基本上允许我将用户路由到特定位置,而无需对我的 htaccess 文件中的每个页面进行硬编码,因此如果用户访问 /about/contact 页面,他实际上会访问 index.php?uri=/about/contact。
问题是有时我希望将问号保留在 $_GET 中。让我们说一个主题标题“怎么了?”那么我的网址会搜索 /topic/what-s-up 之类的主题?并且会匹配what-s-up?在数据库中。但是,现在,我的 $_GET 变量只存储“what-s-up”(没有“?”),而我的数据库仍然存储“what-s-up?” (带有“?”),这表示实际上没有具有该标题的主题。
我怎样才能保持问号如此/topic/what-s-up?仍然转换为 /topic/what-s-up?在查询字符串中?
编辑:用于测试目的的完整 .HTACCESS 文件
Options -Indexes
DirectoryIndex index.html index.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?uri=$1 [QSA,L]
【问题讨论】:
-
?是 URL 中的保留字符,不应用作 slug 的一部分 -
@naththedeveloper 对,但如果我还想保留它怎么办?
-
在 url 中使用:%3F 将其设为
?...urlencode,因此您的 url/topic/what-s-up?将是:/topic/what-s-up%3F -
-
我尝试发送
test.php?asd=?red?并且输出是Array ( [asd] => ?red? ):p 或者重写规则破坏了它?
标签: php apache .htaccess mod-rewrite url-rewriting