Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule, modify querystring

is modifying the querystring possible?

         

Cipherlad

6:28 am on Dec 5, 2011 (gmt 0)

10+ Year Member



I'd like to modify an incoming querystring to a different key. Here's an example:

http://www.mydomain.com?firstvar=myval


to

http://www.mydomain.com?secondvar=myval


I can't find any examples on how to do this. Everything I've seen treats the querystring key/value pair as inseperable and immutable.

incrediBILL

7:33 am on Dec 5, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



According to the Apache docs:
[httpd.apache.org...]
Note: Query String

The Pattern will not be matched against the query string. Instead, you must use a RewriteCond with the %{QUERY_STRING} variable. You can, however, create URLs in the substitution string, containing a query string part. Simply use a question mark inside the substitution string, to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine a new query string with an old one, use the [QSA] flag.


I'm no expert on this, but give something like this a whirl:

RewriteEngine On
RewriteCond %{QUERY_STRING} firstvar=(.*)
RewriteRule ^/$ /?secondvar=%1

The key to zapping the QUERY_STRING is the '?' in the RewriteRule.

g1smd

7:49 am on Dec 5, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Use a RewriteCond looking at QUERY_STRING.

Capture the value part in %1 and re-use it in the rule.

Cipherlad

2:13 am on Dec 6, 2011 (gmt 0)

10+ Year Member



The solution posted by incredibill worked! Thanks!

lucy24

2:33 am on Dec 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteCond %{QUERY_STRING} firstvar=(.*)

Assuming for the sake of discussion that firstvar is either the very last item in your query string, or that everything after it will remain unchanged. And, either way, that anything before firstvar gets thrown away. In that case the .* is a pretty neat alternative to ([^&]+) followed by a whole separate capture leading merely to %1%2 ;)

If, on the other hand, there is stuff before firstvar that you need to keep, it all grows exponentially messier. Unless you just say [QSA] and ignore the old "firstvar", letting it die a natural death. If so, make sure to tell all search engines to ignore it too.

incrediBILL

3:36 am on Dec 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I find working with redirects that rearrange name value pairs in the QUERY_STRING easier to do in scripts than in Apache. I'd do anything more complicated than this in PHP, but that's just me ;)

lucy24

7:48 am on Dec 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Well, this is a fairly unusual question. And that's not something you hear every day in this forum ;) What you see a lot more of is redirecting to pseudo-addresses where everything is in the body of the url, like

www.example.com/green-widgets

and then rewriting to tuck it all into a query string like

www.example.com/uglyurl.php?product=widget&color=green

Or trying to dump some parameters altogether. Which can usually be dealt with simply by telling the search engines to ignore them until they go away.

I don't think we ever delved into why the query string has to be modified in the public URL. I just know that I consistently misread the question as "modulate into a different key".

g1smd

8:57 am on Dec 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month





 RewriteCond %{QUERY_STRING} firstvar=(.*) 


This will DUMP all variables before firstvar and capture all variables after firstvar.

Is that what you want?