.htaccess query string redirect

Discussion in 'Apache' started by MrThePlague, Dec 26, 2007.

  1. #1
    I've been searching on Google and testing for hours, but still haven't made any headway.

    Basically, I'm looking to direct any requests from:
    http://www.example.com/index.php?code=whatever to http://www.example.com

    I thought this should work:

    RewriteRule ^index.php?code=(.*)$ http://www.example.com/ [R=301]

    But it definetely does not. Can anyone point me in the right direction? Thanks!
     
    MrThePlague, Dec 26, 2007 IP
  2. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #2
    What about this ?

    RewriteCond %{QUERY_STRING} ^code=
    RewriteRule index\.php$ http://domain.com/ [R=301]
    Code (markup):
     
    joebert, Dec 26, 2007 IP
  3. MrThePlague

    MrThePlague Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I should have been a bit more thourough in my question, I apologize. Basically, I want the normal user to see the full url string:

    http://www.example.com/index.php?code=whatever

    But I would like the search engine spiders to be directed to the homepage, so:
    http://www.example.com

    I have it pretty close with the following:

    RewriteCond %{HTTP_USER_AGENT} ^Googlebot [OR]
    RewriteCond %{HTTP_USER_AGENT} ^Slurp [OR]
    RewriteCond %{HTTP_USER_AGENT} ^MSNbot
    RewriteCond %{QUERY_STRING} ^code=(.*)$
    RewriteRule ^index.php$ index.php? [R=301]
    RewriteRule ^index.(.*)$ http://www.example.com/ [R=301,L]

    There are a couple issues though. If you are NOT a spider and you go to the full URL string, you get redirected to "http://www.example.com/?code=test" rather than "http://www.example.com". Also, I'm not sure if having the last two redirects they way they are is very efficient.
     
    MrThePlague, Dec 26, 2007 IP
  4. hairulazami

    hairulazami Banned

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I have problem like this URL:

    http://www.macdevcenter.com/pub/a/mac/2004/02/03/latex.html?page=2
    -OR-
    http://www.oreilly.com/pub/a/mac/2004/02/03/latex.html?page=last

    how to made RewriteRule in HTACCESS file, from URL above.

    I try changed my .HTACCESS file before, to make some rewrite URL, then I use this script, to get permalink name of URL:

    $URLID = str_replace(ABSOLUTE_PATH . PHOTOSHOP_PATH ,'', $_SERVER["REQUEST_URI"]); 
    $URLID = str_replace(".html",'', $URLID); 
         
    $sql = "SELECT * FROM ".COM_TUTOR." WHERE 
    comPermalink = '$URLID'"; 
    
    PHP:
    But I want to get the last query (page=2) too
    then I can do this script in PHP:
    note: I want the HTML extention is still used.

    <? if($page=2) { echo "this is page number 2"; } else { echo "this is page number 1"; } ?>
    PHP:
    thanks :)
     
    hairulazami, May 13, 2008 IP