Warning: session_start() [function.session-start]: open(/home/48/08/3030848/web/libs/../../.sessions//sess_ef2a300d4b96dd005d3d33878c600087, O_RDWR) failed: No such file or directory (2) in /home/48/08/3030848/web/libs/session.php on line 14

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/48/08/3030848/web/libs/session.php:14) in /home/48/08/3030848/web/libs/session.php on line 14

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/48/08/3030848/web/libs/session.php:14) in /home/48/08/3030848/web/libs/session.php on line 14
How to make mod_rewrite strip a query string
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

Warning: Unknown(): open(/home/48/08/3030848/web/libs/../../.sessions//sess_ef2a300d4b96dd005d3d33878c600087, O_RDWR) failed: No such file or directory (2) in Unknown on line 0

Warning: Unknown(): Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/home/48/08/3030848/web/libs/../../.sessions/) in Unknown on line 0