Solved

Regular expression for URL Rewrite

Posted on 2010-11-29
7
552 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Free NetCrunch network monitor licenses!

Only on Experts-Exchange: Sign-up for a free-trial and we'll send you your permanent license!

Here is what you get: 30 Nodes | Unlimited Sensors | No Time Restrictions | Absolutely FREE!

Act now. This offer ends July 14, 2017.

 
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 75

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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…

729 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