views:

96

answers:

3

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?

A: 

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

RewriteEngine on
RewriteRule ^go/([^/\.]+)/?$ /go.php?page=$1 [QSA]
Nick
I've tried that, and get the same result "oh shnizzle!"
Cheryl
I should also mention that when I use [QSA], a normal query string doesn't get passed along either. Do you know if there are specific mod_rewrite config settings that completely turn off GET all-together?
Cheryl
This isn't what QSA fixes - that simply allows any query string on the original url to get put into the rewritten URL
Paul Dixon
Paul, you're right - sorry, didn't read the question properly.
Nick
A: 

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.

Lucanos
+2  A: 

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.

Tim Stone
Yep, this is probably it.
ceejayoz
Excellent! Yes, this is it. The rewrite is working perfectly fine now, and the $_GET params are coming through. Thank you!
Cheryl
Good stuff, glad to hear that it's working.
Tim Stone