Regular expression that matches on optional single querystring parameter - regex

This IIS rewrite rule:
<rule name="music search state">
<match url="^music/state/([a-zA-Z-+']+)?$"/>
<action type="Rewrite" url="search.aspx?stateurl={R:1}&t=2" appendQueryString="true"/>
</rule>
Matches on URL:
www.example.com/music/state/north-dakota
But not on URL:
www.example.com/music/state/north-dakota?country=usa
I also tried:
^music/state/([a-zA-Z-+']+)? and ^music/state/([a-zA-Z-+']+)
But none of these work on both URLs...what do I need to change in this expression so it matches on both?
update
I tried extending the last part of the expression, but these 2 expressions throw a Configuration file is not well-formed XML error in IIS:
([a-zA-Z-+'?\&=]+)
([a-zA-Z-+'?&=]+)
and this expression does not match the URL ([a-zA-Z-+'?&=]+)

In the regex ^music/state/([a-zA-Z-+']+)?$ part [a-zA-Z-+'] is responsible for all characters that can be found after music/state/.
Something like this:^music/state/([a-zA-Z-+'?&=]+)?$
BTW, you can check you regex using online interpreter, like http://regexr.com/

Related

I need help making this regular expression to match part of url

I am writing a rewrite rule in web.config file and want to match against a url (using regular expression) if it containes:
*/admin*
So as long as the url contains above it should match. Example of legal matches:
http://test.com/admin
https://test.com/admin
http://test.com/admin/
http://test.com/admin/test
http://test.com/admin/grgr/hht/
Example of illegal matches:
http://test.com
https://test.com/adminpage
https://test.com/adminpage/
I have tried the followings without success:
<match url="(.*)/admin$" ignoreCase="false" />
<match url="/admin?" ignoreCase="false" />
<match url=".*/admin?" ignoreCase="false" />
Try this regex
.*admin(\/.*|$)
Try something like this
.*\b\/admin\b.*
https://regex101.com/r/zq0SAy/1
Details:
\b asserts position at a word boundary

split URL using Regular Expression in IIS

I am facing challenges in splitting the URL using regular expression.
I want to change the mid of the URL part since we changed the URL of the site pages.
https://test.company.com/about/news/2015/test/award.aspx
The above given URL needs to replace as below,
https://test.company.com/en/about/media/news/2015/test/award.aspx
I want to achieve this functionality using Regular Expression in IIS.
I tried the code as below in URL Rewrite in IIS,
about/news/2015(.*.+?)
Help to resolve this as required, thanks in advance.
The regex engine gets the string without the host and protocol, starting with about. Thus, you need to match starting with this fixed string, capture the parts between which you need to insert the required value and use
^(about)(/news/2015/)
Replace with
en/{R:1}/media{R:2}
Where {R:1} refers to about and the {R:2} refers to /news/2015/.
Here is a demo of how this regex works.
You could try below rule:
<rule name="rule11-1" stopProcessing="true">
<match url="^about/news/2015/(.*)" />
<conditions>
<add input="{REQUEST_URI}" pattern="^/about/(.*)" />
</conditions>
<action type="Redirect" url="https://test.company.com/en/about/media/{C:1}" />
</rule>

What's wrong my url rewrite regex

I use url rewrite. I add some rule like that:
<add name="Homes" virtualUrl="^/(.*).html" rewriteUrlParameter="ExcludeFromClientQueryString" destinationUrl="/Default.aspx?vsm=$1" ignoreCase="true" />
<add name="HomeNew" virtualUrl="^/(.*)/(.*)/" rewriteUrlParameter="ExcludeFromClientQueryString" destinationUrl="/Default.aspx?vsm=$1&idcnew=$2" ignoreCase="true" />
<add name="HomeNewPage" virtualUrl="^/(.*)/(.*)/page-([0-9-]*).htm" rewriteUrlParameter="ExcludeFromClientQueryString" destinationUrl="/Default.aspx?vsm=$1&idcnew=$2&page=$3" ignoreCase="true" />
<add name="HomeNewNew" virtualUrl="^/(.*)/(.*)/(.*)-([0-9-]*).htm" rewriteUrlParameter="ExcludeFromClientQueryString" destinationUrl="/Default.aspx?vsm=$1&idcnew=$2&idnew=$4" ignoreCase="true" />
I have to use all of them.
In HomeNewPage rule, I use to get catalog new page. In HomeNewNew rule, I use to get content new with $3 is url name of new.
But when go to this link: "/News/Alert/page-2" my request is "vsm=News&idcnew=Alertpage-2"
I want my request is "vsm=News&idcnew=Alert&page=2"
Please help me! What's wrong? And how to fix it?
If the url is like
"/Default/News/Alert/page-2.html"
Regex:- /(.*)/(.*)/(.*)/page-(.*).html
"/Default/News/Alert/page-2"
Regex:- /(.*)/(.*)/(.*)/page-(.*)
and you can make the new url as /Default.aspx?vsm=$2&idcnew=$3&page=$4
Basically each (.*) specify the argument like
Default is $1
News is $2
Alert is $3
But we've hardcoded the page- so its not an argument. So, (.*) after page- comes out to be argument $4
If we take your second expression ^/(.*)/(.*)/. In this case it may creates the problem with url like News/Alert/page-2 because it doesn't "/" in the end. Same is the reason with .html.
As you've not provided the url so I'm not sure about ^. But this will come only if your url will fulfill the regex from first character, however, it wont leads to any problem. You can specify or remove it.
According to me your Regex expression:
/([a-zA-Z0-9-]*)/([a-zA-Z0-9-]*)/page-([0-9-]*)\.htm
will work. But this expression
/([a-zA-Z0-9-]*)/([a-zA-Z0-9-]*)/"
wont work as it does consist of arguement for third tag(page-2). But this will work till /News/Alert. Please check your code or try to debug it according to me there is some hardcoding done. Due to which you're getting error.
I'll give you a very easy way to detect your expressions. After this you'll be able to validate your expressions appropriately.
Go to this link : http://www.rexfiddle.net/
And insert your expression and in second window insert the url you want. After this you'll get captures in the end linke capture 1, capture 2, capture 3. These will be come out to be your arguements($1, $2......). See the screenshot below:

Regex URL rewriting for custom login page

I'm implementing a custom login page for a multitenant portal where each client gets a different login page styled according to their stored settings.
To achieve this I am using IIS 7.5 with the URL Rewriting module.
My idea is to capture requests for "http://portal.com/client1/" and rewrite them into "http://portal.com/login.aspx?client=client1".
What I'm struggling with is the regex expression to match the URL and extract the "client1" bit out.
EXAMPLES:
"http://portal.com/pepsi" = "http://portal.com/login.aspx?client=pepsi"
"http://portal.com/fedex" = "http://portal.com/login.aspx?client=fedex"
"http://portal.com/northwind" = "http://portal.com/login.aspx?client=northwind"
"http://portal.com/microsoft/" = "http://portal.com/login.aspx?client=microsoft"
So the match should be found if the requested URL contains a single word after the first "/" and work whether there is a trailing "/" or not.
"http://portal.com/clients/home.aspx" would be ignored by the rule.
"http://portal.com/clients/catalog" would be ignored by the rule.
"http://portal.com/products.aspx" would be ignored by the rule.
Assuming:
That the parameter name is always client
and that you don't care about what is after /client1/ then you can use this simple pattern to capture that portion of the URL and then repeat it as a parameter
here:
<rewrite>
<rules>
<rule name="client1 rewrite">
<match url="^([^/.]*)[/]*$" />
<action type="rewrite" url="login.aspx?client={R:1}"/>
</rule>
</rules>
</rewrite>
Fiddle
This works because in all of the ignore list there is a slash in the "middle", but the slash is optional at ending of the "filter" list. So {R:1} will contain everything up to the first slash or end of url if there is no slash.

Regular expression for replace?

I am trying to replace relative url from /abc/bbc.do?count=1 with /bbc.do?count=1 using urlrewritefilter but when I am using the regular expression /abc/(.*)$ and redirect to /1$ it only seems to be capturing bbc.do and not url parameters. Any suggestions?
<rule match-type="regex">
<from>^/abc/(.+)$</from>
<to type="redirect">/$1</to>
</rule>
Update
It looks like the begin anchor is fine, but the backreferences use % instead of $. Solution is updated.
<rule match-type="regex">
<from>^/abc/(.+)$</from>
<to type="redirect">/%1</to>
</rule>
Also, make sure to add use-query-string="true" to your urlrewrite node. It is false by default.
<urlrewrite use-query-string="true">