What's wrong with this?

Discussion in 'Apache' started by AHA7, May 14, 2007.

  1. #1
    Hello,

    What's wrong in the following rewrite code:

    RewriteRule ^dir/page/get.php?id=7785&title=newpr$ http://www.newdomain.org/page7785/ [R=301,L]
    Code (markup):
    I want to 301 redirect "http://domain.com/dir/page/get.php?id=7785&title=newpr" (this is not a real page) to "http://www.newdomain.org/page7785/"

    It didn't work. Even when I used Redirect 301 instead of the mod_rewrite rule it didn't work either. I guess the problem is with the "?" in the first part (the redirected URL). How can I do it?
     
    AHA7, May 14, 2007 IP
  2. AHA7

    AHA7 Peon

    Messages:
    445
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    OK, I got this:

    RewriteCond %{QUERY_STRING} ^id\=7785\&title\=newpr$
    RewriteRule ^dir/page/get.php$ http://www.newdomain.org/page7785/? [R=301,L]
    Code (markup):
    The above worked just fine, but my question is: If I remove the "?" from the end of "http://www.newdomain.org/page7785/" in the second line it would attach ?id=7785&title=newpr to the new URL so it would redirect to "http://www.newdomain.org/page7785/?id=7785&title=newpr" but when I add the "?" after the target URL in the second line (as it appears above) it would redirect to "http://www.newdomain.org/page7785/" and NOT "http://www.newdomain.org/page7785/?" but why so? what does the "?" at the end of the target URL mean?
     
    AHA7, May 14, 2007 IP
  3. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It means you're overwriting the query string (i.e. everything after the ? in a URL) - by default, if there is no query string specified in the destination, it will keep the query string from the original request when rewriting.
    RewriteRule page.html page.php
    page.html?pie=good -> page.php?pie=good

    If you do specify a query string in your destination, this will overwrite the original.
    RewriteRule page.html index.php?requested=page.html
    page.html?pie=good -> index.php?requested=page.html

    If you want to add to the query string so original data is not lost, you use the QSA flag (query string append):
    RewriteRule page.html index.php?requested=page.html [QSA]
    page.html?pie=good -> index.php?requested=page.html&pie=good

    And if you need to remove the query string all together, just overwrite it with a blank string by sticking a ? onto the end of the destination:
    RewriteRule page.html index.php?
    page.html?pie=good -> index.php
     
    rodney88, May 15, 2007 IP
  4. AHA7

    AHA7 Peon

    Messages:
    445
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks a lot rodney88! That was very helpful!
    Sorry it won't allow me to add rep to your post!
     
    AHA7, May 15, 2007 IP