PDA

View Full Version : Apache Additional GET variables with RewriteRule



Icy Penguin
August 9th, 2008, 05:23 PM
Howdy everyone,

I am trying to get my RewriteRules working compeltely.

Right now I have this:

RewriteRule ^([a-zA-Z0-9]+)$ $1.php
RewriteRule ^([a-zA-Z0-9]+)/$ $1.php

That works fine. As does this:

RewriteRule ^books/wanted$ books.php?category=wanted
RewriteRule ^books/wanted/$ books.php?category=wanted
RewriteRule ^books/unwanted$ books.php?category=unwanted
RewriteRule ^books/unwanted/$ books.php?category=unwanted
RewriteRule ^books/all$ books.php?category=all
RewriteRule ^books/all/$ books.php?category=all

However, if I navigation to books/all/?some_other_variable=wtf it doesn't work - $_GET["some_other_variable"] doesn't exist.

Any help here would be much appreciated.

evildrummer
August 9th, 2008, 05:30 PM
RewriteRule ^books/wanted/?\??(.*)?$ books.php?category=wanted&$1

This should pretty much, not really tested at all,

Theory behind it
^books/wanted - Url should begin books/wanted
/? - It can end with or without a forward slash
\?? It can have a ? in the URL although not a must
(.*)? It doesn't have to have extra characters although it can and gets passed as the more get

Icy Penguin
August 9th, 2008, 06:22 PM
Nothing doing with that yet - argh. One would think that would work though.

Another question then - is the \?? is similar to the /? - just the ? needs to be escaped with a backslash?

evildrummer
August 9th, 2008, 06:36 PM
the \? means a '?' because ? is a regex character, try this: (A much simpler approach)

RewriteRule ^books/wanted/$ books.php?category=wanted
RewriteRule ^books/wanted/?(.*)$ books.php?category=wanted&$1

Icy Penguin
August 9th, 2008, 07:24 PM
Still not working - is it possible that GET vars are ignored?

evildrummer
August 9th, 2008, 07:51 PM
I'm idiotic, try:

RewriteRule ^books/wanted/$ books.php?category=wanted
RewriteRule ^books/wanted/\?(.*)$ books.php?category=wanted&$1 --- (Backslashed the ?)

Icy Penguin
August 9th, 2008, 08:29 PM
This is ridiculous. Still not working. There's just gotta be something with Apache that's preventing this, 'cause I've been working this for the past two hours and it hasn't worked.

Thanks for the help.

Voetsjoeba
August 10th, 2008, 06:15 AM
You need to apply the QSA (for QueryString Append) flag to your rules, like so:


RewriteRule ^books/all/$ books.php?category=all [QSA]Additionally, it's often handy to also specify the L (for Last) flag. Otherwise, it will not stop after it finds a match but continue searching the list and possibly find other matches that intervene with the one you actually wanted. Another useful one is NC (for NoCase), so that your URLs work both in upper and lower case.

So in the end, you'll probably want to go with:


RewriteRule ^books/all/$ books.php?category=all [L,NC,QSA]See the Apache RewriteRule documentation (http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriterule) for more info.

Icy Penguin
August 10th, 2008, 12:19 PM
Yay! It works! Thank you!