Complex query string structure

New to mod_rewrite? This is a good place to start.

Complex query string structure

Postby rem » Wed Oct 14, 2009 4:33 am

Hello,

I can deal with basic mod rewrite rules and up until now that was quite enough but right now, i am stuck for the last few days with something I cannot pull out by my own.

I have few different key=value pairs in the query and a fixed rule doesn't help because the query can change.

e.g:

Code: Select all
test.php?key1=$1&key2=$2&key3=$2


can also be

Code: Select all
test.php?key3=$3&key2=$2


or

Code: Select all
test.php?key2=$2&key1=$1


etc ...

Is there a way around this without writing rules for each case (having in mind possibility for more than 3 or 4 key=value pairs, that would be quite a lot)?

Thank you very much.

Best,
rem.
rem
 
Posts: 3
Joined: Wed Oct 14, 2009 3:21 am

Postby richardk » Wed Oct 14, 2009 2:19 pm

Can you provide some examples?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby rem » Wed Oct 14, 2009 2:34 pm

richardk wrote:Can you provide some examples?


Sure, something like this:

Code: Select all
/cat/cars/sort/price:asc/pg/3
would require the rule
RewriteRule ^cat/(.*)/sort/(.*)/pg/([0-9]+)/?$ index.php?cat=$1&sort=$2&pg=$3 [L]


but the query can change so the rules are changing accordingly.

e.g:
Code: Select all
/sort/price/pg/3
or
/cat/cars/pg/3
or
/pg/5/sort/price:asc/cat/cars


i am wondering, is there a formula to rule out all the queries since there might be quite a few combinations so the rules set might become pretty big?


thank you.
rem
 
Posts: 3
Joined: Wed Oct 14, 2009 3:21 am

Postby richardk » Thu Oct 15, 2009 2:52 pm

You can do it with mod_rewrite, but it's not particularly short (two lines per variable) and you have to list each variable (x3)
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond a=/$1/        ^(a=)(.*/)a/([^/]+)/(.*)$ [NC,OR]
RewriteCond /$1/          ^()(.*)$ [NC]
RewriteCond %1%3&b=?&%2%4 ^([^?]*)\?(.*/)b/([^/]+)/(.*)$ [NC,OR]
RewriteCond %1%3?&%2%4    ^([^?]*)\?(.*)$ [NC]
RewriteCond %1%3&c=?&%2%4 ^([^?]*)\?(.*/)c/([^/]+)/(.*)$ [NC,OR]
RewriteCond %1%3?&%2%4    ^([^?]*)\?(.*)$ [NC]
RewriteRule ^((a|b|c)/.*[^/])/?$ /index.php?%1%3 [QSA,L]

a, b and c are the variables.

I would instead suggest you passed the $1 variable to PHP and looped through it with PHP.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby rem » Thu Oct 15, 2009 3:14 pm

richardk wrote:You can do it with mod_rewrite, but it's not particularly short (two lines per variable) and you have to list each variable (x3)
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond a=/$1/        ^(a=)(.*/)a/([^/]+)/(.*)$ [NC,OR]
RewriteCond /$1/          ^()(.*)$ [NC]
RewriteCond %1%3&b=?&%2%4 ^([^?]*)\?(.*/)b/([^/]+)/(.*)$ [NC,OR]
RewriteCond %1%3?&%2%4    ^([^?]*)\?(.*)$ [NC]
RewriteCond %1%3&c=?&%2%4 ^([^?]*)\?(.*/)c/([^/]+)/(.*)$ [NC,OR]
RewriteCond %1%3?&%2%4    ^([^?]*)\?(.*)$ [NC]
RewriteRule ^((a|b|c)/.*[^/])/?$ /index.php?%1%3 [QSA,L]

a, b and c are the variables.

I would instead suggest you passed the $1 variable to PHP and looped through it with PHP.


thank you richard, is anyhow way shorter than what i have and this gives me a very good insight on how i can build this by myself in the future. i have learned something very important as mod_rewrite is a powerful tool!

best,
rem.
rem
 
Posts: 3
Joined: Wed Oct 14, 2009 3:21 am


Return to Beginner's Corner

Who is online

Users browsing this forum: Google [Bot] and 26 guests

cron