Multiple '?' in rewrite, problem

Topics: User Forum
Oct 8, 2008 at 10:56 AM
I've got this rule which puts a language variable into a querystring param.
RewriteRule ^/(en|fr|es|it|pt)/(.+)((\?.+)|())$ /$2?lang=$1&$3 [L]

so it does this
/en/news/default.asp   -->   /news/default.asp?lang=en

It works fine unless I have other paramaters in the first url
/en/news/default.asp?id=28  --> /news/default.asp?id=28?lang=en

How do i remove or split the URL at '?' so that I can put everything after a '?' into a new variable within iirf?
I'm probably tring to do this in a newbi way so if you have any improvements I'd apreciate it!

Thanks for previous help!
Oct 8, 2008 at 4:03 PM

I would maybe split it into 2 rules.  or maybe three.

One rule to handle the "no param" case.

One to handle the "params, but no lang param" case.

One to handle the case where the params already contain lang.


Oct 10, 2008 at 9:25 AM
Hi Cheeso!

Can you please help me? I've unsucessfuly tried to do what you sugested but am not getting very far!
I now have

# handle case with no query string.
RewriteRule ^/(en|fr|es|it|pt)/(.+).asp(?!\?) /$2?lang=$1 

# handle case with existing query string.
RewriteRule ^/(en|fr|es|it|pt)/(.+)(\?.+)$ /$2?lang=$1&$3

The seccond rule works ok but I cant figure out how to write the first rule. Obviously not that good with regex!
I was trying to use the command

to look for a '?' and run the rule bassed on that.
Oct 14, 2008 at 4:22 AM

If you want the first rule to match a URL that ends in .asp and has no query string, then you can use the $ character to indicate "end of string".

RewriteRule ^/(en|fr|es|it|pt)/(.+).asp$   /$2?lang=$1 

Have you tried the TestDriver.exe ?  It allows you to test out the rules without the server.  It's really useful for developing rules, iteratively testing things out, etc.

Oct 14, 2008 at 9:00 AM
Thanks Cheeso,
I am still a newbie at this especially the regex syntax, even though i have read the documentation! It just didnt sink in with me!
Oct 14, 2008 at 2:02 PM
No problem.
I know Regex syntax can be pretty obtuse.
if you step back from it, it looks like swear words from comic strips.