Development .htaccess rewrite - keep GETs

Discussion in 'Software' started by jezmck, 23 Apr 2007.

  1. jezmck

    jezmck Minimodder

    Joined:
    25 Sep 2003
    Posts:
    4,456
    Likes Received:
    36
    my colleague has a website which currently rewrites page_XX to ?page=XX.
    he'd like it to cope with extra GETs
    e.g.
    page_XX?q=1&w=2
    to
    ?page=XX&q=1&w=2

    but we've struggled with getting the extra GETs to even register with the PHP.

    suggestions highly appreciated.
     
  2. Lazlow

    Lazlow I have a dremel.

    Joined:
    8 Aug 2003
    Posts:
    1,464
    Likes Received:
    0
    I don't know if it's any help, but I've got a .htaccess which rewrites blog.slickhouse.com to slickhouse.com - and it deals with any subpages/arguments. So, if you went to blog.slickhouse.com/something, it'll rewrite it to slickhouse.com/something - the same with PHP pages.

    Code:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^blog.slickhouse.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.blog.slickhouse.com$
    RewriteRule ^(.*)$ http://slickhouse.com/$1 [R=301,L]
    
     
  3. Jamie

    Jamie ex-Bit-Tech code junkie

    Joined:
    12 Mar 2001
    Posts:
    8,180
    Likes Received:
    54
    Something like...
    Code:
    RewriteRule ^page_(.*?)?q=(\d+)&w=(\d+)$ page=$1&q=$2&w=$3
     
  4. jezmck

    jezmck Minimodder

    Joined:
    25 Sep 2003
    Posts:
    4,456
    Likes Received:
    36
    unfortunately the extra variables are variable, so he can't rely on anything which specifies them.

    would .*? make sense? wouldn't that mean: any char;zero or more times;zero or one times...
     
  5. Jamie

    Jamie ex-Bit-Tech code junkie

    Joined:
    12 Mar 2001
    Posts:
    8,180
    Likes Received:
    54
    Code:
    RewriteRule ^page_(.*?)?(.*)$ page=$1&$2
     
  6. jezmck

    jezmck Minimodder

    Joined:
    25 Sep 2003
    Posts:
    4,456
    Likes Received:
    36
    wouldn't the question mark need escaping?
     
  7. Jamie

    Jamie ex-Bit-Tech code junkie

    Joined:
    12 Mar 2001
    Posts:
    8,180
    Likes Received:
    54
    yes, it's not a copy and paste solution.
     
  8. jezmck

    jezmck Minimodder

    Joined:
    25 Sep 2003
    Posts:
    4,456
    Likes Received:
    36
    -
     
    Last edited: 6 Apr 2008
  9. jezmck

    jezmck Minimodder

    Joined:
    25 Sep 2003
    Posts:
    4,456
    Likes Received:
    36
    I have this problem again - any further ideas anyone?
     
  10. hacker 8991

    hacker 8991 What's a Dremel?

    Joined:
    8 Jan 2004
    Posts:
    643
    Likes Received:
    1
    If page_xx?q=1&w=2 is being parsed by PHP, you can (maybe) do this:
    PHP:
    $parsed parse_url($_SERVER['REQUEST_URI']);
    $page "page=" substr($parsed['path'], -2);
    $get = ($parsed['query'] != null) ? "&" $parsed['query'] : "";
    header("Location: ?{$page}{$get}");
     
  11. jezmck

    jezmck Minimodder

    Joined:
    25 Sep 2003
    Posts:
    4,456
    Likes Received:
    36
    heh, I had always assumed that $_SERVER['REQUEST_URI'] would be rewritten too, but it's not. so thank you!
     

Share This Page