Removing the Query String from a .htaccess Redirect

September 17th, 2010 - Posted by Steve Marks to PHP, Web Development.

Are you doing a URL rewrite using a .htaccess file but the query string is being carried through also to the new URL, even though you haven’t specified it using QSA (Query String Append)?

To prevent this additional query string being appended to the destination address simply add a question mark to to the new address. A quick example might be:

RewriteRule ^pages/.*$ [R=301,L]

Voila, no more query string :) Now wasn’t that simple!

This entry was posted on Friday, September 17th, 2010 at 9:40 pm by +Steve Marks and is filed under PHP, Web Development. You can follow any responses to this entry through the RSS 2.0 feed.

Fear not, we won't publish this

Comments (12)
  1. Damn, I was looking for this and been wasting my time for this. It’s this simple and the other sources are very complicated. Thanks bro, love this post :D

  2. MundoCaco says:

    I love ti! Thanks a LOT ^_^

  3. Bill says:

    Augh! I thought I had done this at first — HOURS AGO!!!

    Now, it works! to my great relief.

    Thank you very much!

    Now, I am having trouble with %20 outputs to URL as just 0

  4. Alex says:

    Thank you very much!

  5. Steve Marks!!!

    Saved me with a question Mark!! Thank you so much. I can’t believe how long it took me to redirect a folder to a single page.


  6. Manuel says:

    A questionmark!!!!! Damn, i lost a lot of time looking for this :)

  7. Amit says:

    Thanks man. This really helped.

  8. Fred says:

    Thanks so much, spent the last 15 minutes with various different tabs open trying to solve this problem and it ends up being as simple as adding a question mark! Ridiculous! Thanks

  9. @Jignesh – You can acheive what you’re after by adding the following to yout .htaccess file:

    RewriteEngine On
    RewriteCond %{THE_REQUEST} ^.*/personalcollection.html
    RewriteRule ^(.*)personalcollection.html$ [R=301,L]

    Hope that helps :)

  10. Jignesh says:

    What would be the rule for example I want to redirect the URL


    So basically want to redirect all Urls containing personalcollection.html to home page of site.

  11. Das says:

    Great piece of code. This absolutely works for me. Thanks a lot! Keep up the great work…

  12. Morten says:

    Great! Was searching for this. Thank you :)