mod_rewrite wont pass parameters

I am trying to set up mod_rewrite on my server using various tutorials I have found, but in each case, it redirects to the correct file, but the parameters are not passed.

Take for example this tutorial: http://articles.sitepoint.com/article/guide-url-rewriting/2

I created .htaccess and added the code:


RewriteEngine on
RewriteRule /products/([0-9]+) products.php?id=$1 [L]

If I navigate to products/1234 I am redirected to the file products.php, but when I do echo $_GET[“id”] it fails. Also outputting $_SERVER[‘QUERY_STRING’] also spits out blanks.

What am I missing?

Your .htaccess seems fine.
I’ve had problems in the past where rewriting /something/ to something.php didn’t work, because for some reason unkonw to me Apache was rewriting /something/ to something.php without using the rewriterule (which I could verify by removing the rewriterule and doing the same request, which still worked).
When I changed something.php to _something.php it worked.

So you might try renaming products.php to _products.php and update the .htaccess to


RewriteEngine on
RewriteRule /products/([0-9]+) _products.php?id=$1 [L]

Naw, haven’t you learned about the QSA flag? It’ll retain your previous query string when you create a new one in your redirection (see signature’s tutorial re flags and examples).

Regards,

DK

Yes, I know the QSA flag, but with the problem I described that also didn’t work, since Apache seemed to think

“Hey, there is a request to /products/ and here is a products.php, lets serve that one!”

And not using my rewriterules to achieve that. Hence adding QSA wouldn’t matter, since Apache wasn’t using the RewriteRule in the first place.
Come to think of it, I think Apache was rewriting /products/ to /products.php before applying the .htaccess, so that when the .htaccess was processed /products/ didn’t match (because it now was products.php).

I know this sounds seriously weird, and I was completely puzzled when this happened to me, but renaming products.php to _products.php really solved it for me.

PS. This was on an out-of-the box Apache install on SuSe I believe.

I guess having previous lines changing things would have some effect. :wink:

Hopefully the file is small enough so you can spot it/them easily enough.

Gavin and Scallio,

Mea Culpa! I did my usual scan reading of the first two posts and made an @$$ out of myself!

Okay, you did NOT start with a query string so QSA was WAY off the mark!

What it APPEARS that you have happening is a “capture” of the products.php script by Options MultiViews. Add

Options -MultiViews

at the top of your .htaccess and see if that doesn’t resolve the problem (as ScallioXTX said, your code is fine).

Then go to Apache.org and see what MultiViews does for you. As for me, having a script hijack a URI like you’re seeing is a major PITA - sufficient for me to NEVER use MultiViews!

Again, sorry for not reading the posts thoroughly before sticking my foot in my mouth like that!

Regards,

DK

Ah yes, MultiView seems to do exactly what I described.
At least now I know what to look for the next time this happens to me :tup:

yup, that cured it! What a PITA!

Thanks!

Oh, i had to change a couple other things too to get it to work, the final code was:


Options -MultiViews
RewriteEngine on
RewriteRule ^products/([0-9]+)$ products.php?id=$1

Aw, Gavin, where’s your LAST flag?

Regards,

DK