Email: Password:
Don't have a login? Just enter your email and the password you want.
Your email will NOT be publicly displayed or given away. More Info

dranger.com

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 comment