让 mod_rewrite 传递 $_GET 参数?

发布于 2024-09-08 23:50:41 字数 513 浏览 0 评论 0原文

我在这里读过关于同一主题的其他几个问题,但我的略有不同。我正在尝试做一个非常基本的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三生路 2024-09-15 23:50:41

您可能已打开MultiViews。将其添加到 .htaccess 文件的顶部:

Options -MultiViews

希望问题应该消失。

为了详细说明在这种情况下发生的情况,您的 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:

Options -MultiViews

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, so MultiViews transforms it into /go.php, which does exist. When this happens, the /somewhere bit is passed to PHP as $_SERVER['PATH_INFO'], but go.php doesn't match your rewrite rule, so the rewrite is not performed to write that query string.

我偏爱纯白色 2024-09-15 23:50:41

您的重写规则上需要 QSA(查询字符串附加)标志。

RewriteEngine on
RewriteRule ^go/([^/\.]+)/?$ /go.php?page=$1 [QSA]

You need the QSA (query string append) flag on your rewrite rule.

RewriteEngine on
RewriteRule ^go/([^/\.]+)/?$ /go.php?page=$1 [QSA]
蒲公英的约定 2024-09-15 23:50:41

一些想法...

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^/go/([^/\.]+)/?$ /go.php?page=$1 [QSA]

如果请求有效的页面、脚本或目录,这应该会阻止 mod_rewrite 规则被触发。它还将附加任何现有的查询字符串。

go.php 文件中,我将具有以下内容:

<?php
ini_set('display_errors',1);
echo '<b>$_GET Variables</b><pre>';
var_dump( $_GET );
echo '</pre>';
?>

这样,您可以准确地看到哪些 GET 变量被传递给脚本。

A few ideas...

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^/go/([^/\.]+)/?$ /go.php?page=$1 [QSA]

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:

<?php
ini_set('display_errors',1);
echo '<b>$_GET Variables</b><pre>';
var_dump( $_GET );
echo '</pre>';
?>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文