Removing query string with RewriteRule/RewriteCond

Discussion in 'Apache' started by stevenswing, Oct 19, 2009.

  1. #1
    We currently have:

    postp154336.html&highlight=

    However, we want to redirect them to just postp154336.html

    Please be aware that the URLs are changing, for example postp12345.html - the bold can change.

    Thank you for your help.
     
    Last edited: Oct 19, 2009
    stevenswing, Oct 19, 2009 IP
  2. stevenswing

    stevenswing Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    We also want to remove:

    postt10093.html&view=next
    postt10093.html&view=previous

    However, we only want to remove those strings, not ALL strings from the end of the URLs.

    Thank you.
     
    stevenswing, Oct 19, 2009 IP
  3. chadsmith

    chadsmith Peon

    Messages:
    82
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Something like this should do it:

    RewriteCond %{QUERY_STRING} ^highlight=$
    RewriteRule ^postp([0-9]+)\.html$ /postp$1.html? [R=301,L]
    
    RewriteCond %{QUERY_STRING} ^(.*&?)(view=(next|previous))&?(.*)$
    RewriteRule ^postt([0-9]+)\.html$ /postt$1.html?%1%4 [R=301,L]
    Code (markup):
     
    chadsmith, Oct 19, 2009 IP
  4. stevenswing

    stevenswing Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank you for your reply. Unfortunately, that didn't work. To make things even more complicated, the actual postt10093.html is a RewriteRule itself using this rule:

    RewriteRule ^post(p|t)([1-9][0-9]*).html$ modules.php?name=Forums&file=viewtopic&$1=$2
    Code (markup):
    Would this make a difference?

    Thank you again.
     
    stevenswing, Oct 20, 2009 IP
  5. stevenswing

    stevenswing Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    chadsmith, do you have any ideas on what can be done?

    I really appreciate your help.
     
    stevenswing, Oct 23, 2009 IP
  6. chadsmith

    chadsmith Peon

    Messages:
    82
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #6
    It should work if you put my rules before it. Make sure it has the [QSA] flag at the end of that last one though so it sends any additional query strings.

    Something like:

    RewriteCond %{QUERY_STRING} ^highlight=$
    RewriteRule ^postp([0-9]+)\.html$ /postp$1.html? [R=301,L]
    
    RewriteCond %{QUERY_STRING} ^(.*&?)(view=(next|previous))&?(.*)$
    RewriteRule ^postt([0-9]+)\.html$ /postt$1.html?%1%4 [R=301,L]
    
    RewriteRule ^post(p|t)([1-9][0-9]*).html$ modules.php?name=Forums&file=viewtopic&$1=$2 [L,QSA]
    Code (markup):
     
    chadsmith, Oct 26, 2009 IP
  7. stevenswing

    stevenswing Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thank you for your reply.

    It's still not working and just returning a 404. I have placed your RewriteRules before the existing one, I have appended the QSA, I have even removed the $ from the original RewriteRule, which fixes the 404 of course, but still does not redirect to the page with the stripped query string.

    I'm lost - any ideas?

    Thank you again.
     
    stevenswing, Oct 27, 2009 IP
  8. stevenswing

    stevenswing Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Any ideas?
     
    stevenswing, Nov 2, 2009 IP
  9. chadsmith

    chadsmith Peon

    Messages:
    82
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #9
    PM me a few of the URLs that aren't working.
     
    chadsmith, Nov 2, 2009 IP