SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Using querystring variable in RewriteRule

  1. #1
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    145
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using querystring variable in RewriteRule

    Hi there

    I was hoping someone could give me a solution to the following problem as I'm stuck....

    I have a .htaccess file which I use to rewrite search engine friendly URLs into PHP URLs so that the script can use the $_GET values. It looks something like this:

    Code:
    RewriteEngine On
    RewriteRule ^([a-zA-Z0-9-]+)-([0-9]+)-([0-9]+).html$      product_list.php?cid=$2&bid=$3 [L]
    What I want to do is also be able to read querystring variables that may be appended to the search engine friendly URL. For example, the URL could be:

    CategoryTitleHere-25-3.html?sort=4

    So, I changed the .htaccess file to:

    Code:
    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^sort=([0-9])?$
    RewriteRule ^([a-zA-Z0-9-]+)-([0-9]+)-([0-9]+).html$      product_list.php?cid=$2&bid=$3&sort=%1 [L]
    This works. BUT, it doesn't work if the sort variable isn't present. How can I change the RewriteCond so that it doesn't matter if there isn't a querystring? Also, how can it be amended to include additional querystring variables such as 'page'? Basically, it needs to work with URLs like this:

    CategoryTitleHere-25-3.html
    CategoryTitleHere-25-3.html?sort=6
    CategoryTitleHere-25-3.html?sort=6&page=2

    Any help would be much appreciated

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    If you add the [QSA] flag on your original rule, it'll pass along any query string variables there in addition to the ones you created. mod_rewrite is smart like that, you won't need these additional rules or conditions.
    17-29% of paid ad clicks are fraudulent. Get protected with Improvely, your online marketing dashboard.
    Conversion tracking, click fraud detection, A/B testing and more.

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    145
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Sounds good. But how would I rewrite my existing rule so that it can read the querystring values for 'sort' and 'page', which may or may not be there?

    Many thanks.

    edit:
    Does this look correct:
    Code:
    RewriteRule ^([a-zA-Z0-9-]+)-([0-9]+)-([0-9]+).html([\?sort=0-9]?)$          product_list.php?cid=$2&bid=$3&sort=$4 [QSA,L]
    If so, would I just amend it as follows to include page?:
    Code:
    RewriteRule ^([a-zA-Z0-9-]+)-([0-9]+)-([0-9]+).html([\?sort=0-9]?)([page=0-9]?)$          product_list.php?cid=$2&bid=$3&sort=$4&page=$5 [QSA,L]

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You do not include "sort" or "page" at all

    Use your original rule, just add [QSA] flag

    It will append any query string that is there

    Code:
    RewriteRule ^([a-zA-Z0-9-]+)-([0-9]+)-([0-9]+).html$      product_list.php?cid=$2&bid=$3 [QSA,L]
    17-29% of paid ad clicks are fraudulent. Get protected with Improvely, your online marketing dashboard.
    Conversion tracking, click fraud detection, A/B testing and more.

  5. #5
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    145
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahhh, I see. That is superb, thanks I can't believe I didn't find such a solution when googling. In fact, some solutions I found seemed too complex with no mention of the QSA flag. Obviously I'm new to mod_rewrite, but I'm really starting to love it now. Thanks again!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •