%{QUERY_STRING} the Hivemind
February 20, 2008 12:23 PM   Subscribe

Mod_rewrite & query strings: Just when I thought I had it figured out...

I was wanting to redirect from old files [home|fotos|foo|bar].asp with query string variables id & lid to a new & nicer URL /XXX/[index|pics|foozie|barrie], where XXX was an alias dependant on the two values. So I managed to suss out the following

RewriteCond %{QUERY_STRING} ^id=123&lid=456$ [NC]
RewriteRule ^home\.asp$ /whatever/index? [R=301,NC,L]
RewriteRule ^fotos\.asp$ /whatever/pics? [R=301,NC,L]
RewriteRule ^foo\.asp$ /whatever/foozie? [R=301,NC,L]
RewriteRule ^bar\.asp$ /whatever/barrie? [R=301,NC,L]

RewriteCond %{QUERY_STRING} ^id=456&lid=789$ [NC]
RewriteRule ^home\.asp$ /whatever2/index? [R=301,NC,L]
RewriteRule ^fotos\.asp$ /whatever2/pics? [R=301,NC,L]
RewriteRule ^foo\.asp$ /whatever2/fooozie? [R=301,NC,L]
RewriteRule ^bar\.asp$ /whatever2/barrie? [R=301,NC,L]


which works nicely. Yay me.

But now I discovered some search results where variables other than id & lid are appended to the query string. I don't give a flying rat's arse about these variables. I just want to be redirected to /whatever/index if the old request was for home.asp and id=123 & lid=456.

I've tried
RewriteCond %{QUERY_STRING} ^id=456&lid=789.* [NC]
and
RewriteCond %{QUERY_STRING} ^id=456&lid=789? [NC,OR]
RewriteCond %{QUERY_STRING} ^id=456&lid=789.* [NC]

as well as various other screwups. At this point I'm getting blurry-eyed and have lost track of all the combinations I've attempted.

What's the totally obvious mod_rewrite voodoo I've overlooked?
posted by romakimmy to Computers & Internet (8 answers total)
 
Um, have you tried just id=456&lid=789 , without anything after it?
posted by team lowkey at 12:44 PM on February 20, 2008


Response by poster: Just tried it, and matches the query string when the old request is for home.asp, but for the other files it's defaulting to the first set of rules. i.e. fotos.asp?id=456&lid=789 is redirecting to /whatever/pics instead of /whatever2/pics.

Should I be capturing & referencing the matched query string somehow in the RewriteRule?
posted by romakimmy at 1:06 PM on February 20, 2008


I don't have an answer for your particular question, but I have a suggestion for where you could get one:

#apache on the IRC server irc.openprojects.net

That channel has been, without question, the most useful tech help channel I've ever found on IRC. And mod_rewrite questions are 4/5 the reason I go in there. You can usually get a quick, correct answer without a lot of excessive chatter.
posted by autojack at 1:53 PM on February 20, 2008


I'm not sure I'm following what you're after anymore. Does this get you the result you want?

RewriteCond %{QUERY_STRING} id=123 [NC]
RewriteCond %{QUERY_STRING} lid=456 [NC]
RewriteRule ^home\.asp /whatever/index? [R=301,NC,L]
RewriteRule ^fotos\.asp /whatever/pics? [R=301,NC,L]

RewriteCond %{QUERY_STRING} id=456 [NC]
RewriteCond %{QUERY_STRING} lid=789 [NC]
RewriteRule ^home\.asp /whatever2/index? [R=301,NC,L]
RewriteRule ^fotos\.asp /whatever2/pics? [R=301,NC,L]
posted by team lowkey at 2:20 PM on February 20, 2008


Did you know that the RewriteCond only applies to the next RewriteRule (only the very next one, and not the remaining 3 in each section)?
posted by so at 8:02 PM on February 20, 2008


I think so has identified the problem there. You can stack several RewriteConds together, but they all only apply to the single, following RewriteRule
posted by meta_eli at 8:30 PM on February 20, 2008


Response by poster: Yeah, I found that and the fact that RewriteRule is matched before RewriteCond last night, but it was late to fuzz around any more & I decided to go to bed. (Not that sleeping was restful - I fucking dreamt about about mod_rewrite all night. /cranky)

Since I won't have access to RewriteMap, I'll have to throw in the towel and write a quick script today.
posted by romakimmy at 3:01 AM on February 21, 2008


rats on bad sleep!

Maybe I'm missing the obvious, but couldn't you just "unroll" your lines?

RewriteCond %{QUERY_STRING} ^id=123&lid=456$ [NC]
RewriteRule ^home\.asp$ /whatever/index? [R=301,NC,L]

RewriteCond %{QUERY_STRING} ^id=123&lid=456$ [NC]
RewriteRule ^fotos\.asp$ /whatever/pics? [R=301,NC,L]

RewriteCond %{QUERY_STRING} ^id=123&lid=456$ [NC]
RewriteRule ^foo\.asp$ /whatever/foozie? [R=301,NC,L]

RewriteCond %{QUERY_STRING} ^id=123&lid=456$ [NC]
RewriteRule ^bar\.asp$ /whatever/barrie? [R=301,NC,L]

...

Of course you're right about getting into RewriteMap territory here!

coffee == good ;)

-s.
posted by so at 5:57 AM on February 21, 2008


« Older How can we encourage local bloggers?   |   How to alphabetize a list while ignoring articles? Newer »
This thread is closed to new comments.