htaccess rewrite and GET variables

Discussion in 'PHP' started by asgsoft, May 11, 2010.

  1. #1
    I am trying to get variables from URLs, rewritten by htaccess and which are passed on using GET.

    At the moment I have:

    Options +FollowSymLinks
    RewriteEngine On
    
    RewriteRule ^eating-out.php?sort=(.+)&page=(.*)$ dev1.php?cat=1&sort=$1&page=$2
    PHP:
    Which is getting me a 404 error page when I access eating-out.php.. meaning the page isn't found.

    However, if I use this:

    Options +FollowSymLinks
    RewriteEngine On
    
    RewriteRule ^eating-out.php dev1.php?cat=1
    PHP:
    And try and access eating-out.php?page=2 it won't work.. the value of $_GET['page'] is still nothing.

    Any ideas?
     
    asgsoft, May 11, 2010 IP
  2. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
  3. chadsmith

    chadsmith Peon

    Messages:
    82
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Also replied on NP:

    The first one isn't working because sort and page are part of the query strings.

    You should only need to add [QSA] to append the query string to the second rewrite:

    RewriteRule ^eating-out.php dev1.php?cat=1 [QSA]
    Code (markup):
    or better yet

    RewriteRule ^eating\-out\.php$ /dev1.php?cat=1 [L,QSA]
    Code (markup):
     
    chadsmith, May 11, 2010 IP
    asgsoft likes this.
  4. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #4

    Thank you sooooo much!! your a legend!! Works wonders :D
     
    asgsoft, May 11, 2010 IP