Getting mod_rewrite to pass $_GET params?

人盡茶涼 提交于 2019-11-28 11:47:04

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.

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!