Solved

Regular expression for URL Rewrite

Posted on 2010-11-29
7
544 Views
Last Modified: 2012-05-10
We are trying to write a rewrite rule to take care of the following URLs. After the rewrite the rewritten URL should be as given below. Basically  format=xxx part is to be captured and made a sub-part of the URL with rest of the query string being passed as is. It would not be a problem if format=xxx is also passed in the rewritten URL. As with any URL, none of the query string parameters are mandatory and the format=xxx can be there anywhere in it or be not there at all. If format is not included we want a default (say xml) to be used. The last part may have to be handled completely separately.

Original URLs:

http://whatever.something.com/sgol/search?version=1&format=json&qsvariable=xyz&id=123
http://whatever.something.com/sgol/search?version=2&format=json&qsvariable=xyz&id=123
http://whatever.something.com/sgol/search?version=3&qsvariable=xyz&id=123&format=json
http://whatever.something.com/sgol/search?version=4&qsvariable=xyz&id=123&format=xml
http://whatever.something.com/sgol/search?version=5&qsvariable=xyz&id=123
http://whatever.something.com/sgol/search?format=json&version=6&qsvariable=xyz&id=123

Rewritten URL:

http://whatever.somethingelse.com/somedir/someresource/{format}/search?version=1&qsvariable=xyz&id=123

This is being done in IIS 7 with URLRewrite module and we want to avoid writing a HTTPModule for this if possible. Any ideas are welcome.
0
Comment
Question by:amit_g
7 Comments
 
LVL 13

Expert Comment

by:themrrobert
ID: 34235906
You might be better off editing the search aspx file so that you build a new uri with code, with the possibility of being on the end/middle/ near different variables each time, a simple code would be best, and possibly faster than an overcomplicated regex.
0
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 250 total points
ID: 34235954
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 34235958
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 58

Author Comment

by:amit_g
ID: 34236729
TerryAtOpus,

Thanks, it looks pretty close. I modified it to

^([^?]+)/logs/([^?]+)\?((?!format).)*?&?format=([^&\n\r]*)(.*?)$

as I wanted to capture the "search" also. Then I updates the replace to

http://whatever.somethingelse.com/somedir/someresource/$4/$2?$5

This works and carries over the query string parameters after the "format=". However, any parameters before this are not captured or carried over. $3 only contains the last matched character.

 I am using Regulator (http://sourceforge.net/projects/regulator/) to test for now. If it works there, I would put it in the IIS7 config.
0
 
LVL 58

Author Comment

by:amit_g
ID: 34236749
themrrobert,

Thanks for the suggestion. We won't be able to do that as the application that ends up being responsible for serving the content is exposed in the target URL format (due to the way WCF exposed endpoints) while we want the end user to see the URL in the source format. These are service URLs and we might have to go to our custom URL rewrite engine. The source format is pretty fixed so easy for RegEx, it is just that one of the query string parameter needs to get in the sub-part of the URL and that is making it complicated.
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 34238090
What about the following. I believe this is the correct syntax:

   {R:1}  is "search...?..." up to "format"
   {R:2}  is the value of "format"
   {R:3}  is the remainder of the querystring after "format", if any

Note, there is the possibility of a trailing ampersand in something like:

    version=1&format=json

but TMK, this shouldn't be an issue since there is no key following it. Please forgive me if I'm incorrect  :)
(\w+\?.*?)format=(\w+)&?(.*)

Open in new window

0
 
LVL 58

Author Closing Comment

by:amit_g
ID: 34243261
Thanks to both TerryAtOpus and kaufmed. I would be able to take it from here.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now