Recent Entries
2009-07-15:
Karl Denninger: Bad Math, Bad Statistics: Trying to get a blogger to admit a mistake
2008-07-04:
Drilling in ANWR
2008-03-05:
Delegate Math
2007-09-27:
Typo in the Constitution?
2007-05-27:
An ffmpeg tutorial
2007-05-22:
How to make mod_rewrite strip a query string
2007-05-09:
New Site
Related Entries
programming
2007-05-09:
New Site
2007-05-27:
An ffmpeg tutorial
How to make mod_rewrite strip a query string
2007-05-22
In dealing with my shared hosting account, I had some issues with the PHP settings forcing the session functions to use URL sessionids instead of cookies, so all my links had ?dranger=234fad4c710be etc., on them. This makes Google give me icky looking URLs, so I went in and fixed it by changing the URL rewriting function in PHP.
Unfortunately, Google had already crawled my URLs, so now it has a whole bunch of repeated links in its index of my page (e.g. index.html AND index.html?dranger=234fad4c710be). So I needed to go in and set mod_rewrite (which I'm already using heavily to make my other URLs pretty) to strip the query string of get variables from the request and return that as a 301 Redirect (moved permanently). I didn't see anything described like this in the mod_rewrite docs. Unfortunately, it's not as easy as:
RewriteRule (.*)?dranger= /$1 [R=301]
because the text that RewriteRule is checking doesn't contain the rewrite rule. In addition, this doesn't work, either:
RewriteCond %{QUERY_STRING} dranger= RewriteRule (.*) /$1 [R=301]
because when it rewrites the URL, it appends the query string onto the rewritten URL. What a drag. But I did notice that this:
RewriteCond %{QUERY_STRING} dranger= RewriteRule (.*) /bob.html?x=44 [R=301]
will remove the previous get variable. So I tried this out:
RewriteCond %{QUERY_STRING} dranger= RewriteRule (.*) http://www.dranger.com/$1? [R=301]
which not only stops the query string from being added but removes the single question mark — which is exactly what we wanted!
Comments
Login to leave a commentThanks a lot for this tip. I needed it desperately! My forum archive became much better at SERPs so I thought of redirecting archived threads to the original threads and I spent a week trying to figure out how to remove the querystring!
= = = =
Lawyers
How to replace certain characters in a string? seo
Great tip! I didn´t that we could use the mod_rewrite to strip a query string. I just used it to rewrite urls. Thanks a lot, otimização de sites
Thanks for this tip. I was having the same problem and I was so confused as to how to fix it. You're my new hero! Thanks.
Thanks, this is exactly what I was after Cooking Forum"; userLogin.afterLogin.push( function() { msgCommentOptions("options_115",84, "", 115, 0, "5"); } );
I messed up my last comment, not sure what happened.
Cooking Forum
After having a hard time with both the official and the unofficial mod_rewrite documentation, I'm quite glad I've found your solution. Thanks!
Robert