Optional querystring elements in any order

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

Optional querystring elements in any order

Postby MikkyX » Wed Jul 22, 2009 12:47 am

Hi folks,
I'm OK with simple mod_rewrite rules but this one has me stumped.

We have a page which can accept up to six querystring elements in order to filter what is displayed by price, category, subcategory, subsubcategory (yes, really), brand, and site area.

What we'd like to do is have some sort of mod_rewrite rule which allows us to go to, for example:
/brokerage/a1/p50/c3/sc10/ss15/b4

This would load:
/brokerage.php?area=1&price=50&category=3&subcategory=10&subsub=15&brand=4

But here's the kicker - these six querystring elements could appear in any order, and may not appear at all. The page displays everything to begin with and these options provide a drilldown function.

How on EARTH would I write a RewriteRule to cope with this?
MikkyX
 
Posts: 2
Joined: Wed Jul 22, 2009 12:42 am

Postby richardk » Wed Jul 22, 2009 10:40 am

You would match them in a similar way you would match query string variables but with many variables it could become very long. You should also consider duplicate content issues.

Pure mod_rewrite
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond $1 ^/(.+/)?a([0-9]+)(/.+)?$ [NC,OR]
RewriteCond $1 ^/(.+)$
RewriteCond area=%2#%1%3 ^([^#]+)#(.*/)?p([0-9]+)(/.*)?$ [NC,OR]
RewriteCond area=%2#%1%3 ^([^#]+)#(.+)$
RewriteCond %1&price=%3#%2%4 ^([^#]+)#(.*/)?c([0-9]+)(/.*)?$ [NC,OR]
RewriteCond %1&price=%3#%2%4 ^([^#]+)#(.+)$
RewriteCond %1&category=%3#%2%4 ^([^#]+)#(.*/)?sc([0-9]+)(/.*)?$ [NC,OR]
RewriteCond %1&category=%3#%2%4 ^([^#]+)#(.+)$
RewriteCond %1&subcategory=%3#%2%4 ^([^#]+)#(.*/)?ss([0-9]+)(/.*)?$ [NC,OR]
RewriteCond %1&subcategory=%3#%2%4 ^([^#]+)#(.+)$
RewriteCond %1&subsub=%3#%2%4 ^([^#]+)#(.*/)?b([0-9]+)(/.*)?$ [NC,OR]
RewriteCond %1&subsub=%3 ^(.+)$
RewriteRule ^brokerage((/[^/]+){0,6})/?$ /brokerage.php?%1&brand=%3 [QSA,L]

(Not full tested.)

A different option might be to send the request to PHP to get the variables.
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^brokerage((/[^/]+){0,6})/?$ /brokerage.php?mod_rewrite=$1 [QSA,L]

and something like
Code: Select all
<?php

if(isset($_GET['mod_rewrite']) && !empty(trim($_GET['mod_rewrite'], '/')))
{
  $mr_pieces = explode('/', trim($_GET['mod_rewrite'], '/'));
  foreach($mr_pieces as $mr_value)
  {
    if(strpos($mr_value, 'a') === 0)
    {
      $_GET['area'] = substr($mr_value, 1);
    }
    else
    if(strpos($mr_value, 'p') === 0)
    {
      $_GET['price'] = substr($mr_value, 1);
    }
    else
    if(strpos($mr_value, 'c') === 0)
    {
      $_GET['category'] = substr($mr_value, 1);
    }
    else
    if(strpos($mr_value, 'sc') === 0)
    {
      $_GET['subcategory'] = substr($mr_value, 2);
    }
    else
    if(strpos($mr_value, 'ss') === 0)
    {
      $_GET['subsub'] = substr($mr_value, 2);
    }
    else
    if(strpos($mr_value, 'b') === 0)
    {
      $_GET['brand'] = substr($mr_value, 1);
    }
  }
}
}

(I'm sure it could be shorter.)
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby MikkyX » Thu Jul 30, 2009 1:59 am

Hi Richard,
Sorry it's taken me a while to reply - I forgot to check back here! Thanks for your suggestions, I will give them a try :)
MikkyX
 
Posts: 2
Joined: Wed Jul 22, 2009 12:42 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 30 guests

cron