让 mod_rewrite 传递 $_GET 参数?
我在这里读过关于同一主题的其他几个问题,但我的略有不同。我正在尝试做一个非常基本的 mod_rewrite:
RewriteEngine on
RewriteRule ^go/([^/\.]+)/?$ /go.php?page=$1
go.php
看起来像这样:
<?php
ini_set('display_errors',1);
if(isset($_GET['page'])){
echo 'page='.$_GET['page'];
}else{
echo 'oh shnizzle!';
}
?>
现在,当我在浏览器中转到 /go/someword
时, $_GET
param“someword”未传递,我收到消息“oh shnizzle!”每次。我无法通过 mod_rewrite 传递任何 $_GET
参数的可能原因是什么?
There's a couple other questions on this same topic on here that I've read, but mine is slightly different. I'm trying to do a very basic mod_rewrite:
RewriteEngine on
RewriteRule ^go/([^/\.]+)/?$ /go.php?page=$1
go.php
looks like this:
<?php
ini_set('display_errors',1);
if(isset($_GET['page'])){
echo 'page='.$_GET['page'];
}else{
echo 'oh shnizzle!';
}
?>
Now, when I go to /go/someword
in my browser, the $_GET
param "someword" IS NOT passed along, and I get the message "oh shnizzle!" every time. What are possible reasons I'm not able to pass any $_GET
params through mod_rewrite?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能已打开
MultiViews
。将其添加到.htaccess
文件的顶部:希望问题应该消失。
为了详细说明在这种情况下发生的情况,您的 URL
/go/someword
指向不存在的资源,因此MultiViews
将其转换为/go.php
,它确实存在。发生这种情况时,/somewhere
位将作为$_SERVER['PATH_INFO']
传递给 PHP,但go.php
不匹配您的重写规则,因此不会执行重写来写入该查询字符串。You probably have
MultiViews
turned on. Add this to the top of your.htaccess
file:And the problem should go away, hopefully.
To elaborate a little on what's going on if this is the case, your URL
/go/someword
points to a non-existent resource, soMultiViews
transforms it into/go.php
, which does exist. When this happens, the/somewhere
bit is passed to PHP as$_SERVER['PATH_INFO']
, butgo.php
doesn't match your rewrite rule, so the rewrite is not performed to write that query string.您的重写规则上需要 QSA(查询字符串附加)标志。
You need the QSA (query string append) flag on your rewrite rule.
一些想法...
如果请求有效的页面、脚本或目录,这应该会阻止 mod_rewrite 规则被触发。它还将附加任何现有的查询字符串。
在 go.php 文件中,我将具有以下内容:
这样,您可以准确地看到哪些 GET 变量被传递给脚本。
A few ideas...
This should stop the mod_rewrite rules from being triggered if a valid page, script or directory is requested. It will also append any existing Query Strings.
In the go.php file, I would have the following:
That way, rather than looking for a specific variable (at least until it behaves itself) you can see exactly what GET variables are being passed to the script.