Preserve QueryString in RegEx

Topics: Developer Forum, User Forum
May 7, 2007 at 10:42 PM
I am trying to craft a Regular Expression that will ensure that I don't create two "?" characters when rewriting a URL. Here is an example of what I'm trying to do:




May 8, 2007 at 2:26 AM
Edited May 8, 2007 at 2:27 AM
First of the manual. It pretty much spells everything out for you.

Your first rule would look as such:

RewriteRule ^/(\w)/(\w)/$ /$2/default.aspx?usr=$1

The second rule would look like:

RewriteRule ^/(\w)/(\w)/default.aspx?guid=(\w)$ /$2/default.aspx?guid=$3&usr=$1

I'm assuming that all values will only be letters

The idea is to right as few rules as possible while being generic as possible. Figure out the structure of your site, what parameters you need to pass and incorporate that into the virtual structure you'll setup with Ionics. You have to get out of the idea of physical folders, query strings because your second rule could easily be just like this:


and the page that processes it would be


No need to pass an actual just have to worry about capturing all the variables and translate the path to the actual page that'll process it. It's transparent to the visitor. and nice for Search Engines.

If you need help constructing Regular Expressions I highly recommend Expresso; a freeware RegEx builder/analyzer. Helped me out tremendously! All check out for a RegEx cheat sheet!.

Best of luck!
May 8, 2007 at 5:45 PM
Edited May 8, 2007 at 6:13 PM
Thanks for your assistance..

You are right in that I should use as few rules as possible while staying as generic as possible. To that end I am looking at using the following rule:

RewriteRule ^/([^/]+)(.*)$ /$2?usr=$1

This rule is pretty generic in that it will basically strip off whatever the first directory is and sticks it on the end as part of a query string. Unfortunately, if a query string is already on the URL I end up with two of the "?" characters. I am still searching for the best way to ensure this doesn't happen.

Your suggestion of restructuring my application so that it doesn't use any Query Strings just isn't feasible in my situation. I am working with a legacy application that is making a slow migration. Maybe eventually I'll get there :) For the time being I need a rule that doesn't create two "?" characters.

One idea is to use a RewriteCond that determines is there is a QueryString. If 'true' then append with an '&'. Then use another RewriteCond that determines if there is NOT a QueryString. If 'true' then append with a '?'.

A different option is for me to use a RewriteRule that changes all '?' characters to '&' with the exception of the first. However, I am having trouble creating that RegEx.
May 9, 2007 at 4:03 PM
In the end I took different route...

RewriteRule ^(.*)\?(.*)$ $1&$2
RewriteRule ^/([^/]*)/([^&]*)(.*) /$2?usr=$1$3 [L]