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">
Related
I'm trying to make a IIS redirect rule to redirect from this url pattern, but it beats me:
https://www.mycompanyPLC.com/en/lorem/ipsum/whatever
to
https://www.mycompanyLTD.com/lorem/ipsum/whatever
Basically I need to replace PLC with LTD and if there is the "/en/" group in url, this has to be removed.
You can achieve your both the requirements using the single regex provided /en/ is preceded by .com. Something like:
(.*?)PLC\.com(?:\/\ben\b)?(.*)
Explanation of the above regex:
(.*?) - Represents 1st capturing group capturing everything before PLC lazily.
PLC\.com - Matches PLC.com literally.
(?:\/\ben\b)? - Represents a non-capturing group matching \en literally zero or one time. \b represents a word boundary.
(.*) - Represents the second capturing group matching everything after \en greedily.
$1LTD.com$2 - For the replacement(or redirection in this case) part you can get away with this string where $1 represents the first captured group and $2 represents the second captured group. In your case; you can use {R:1}LTD.com{R:2}.
You can find the demo of the above regex in here.
Please refer to below URL rule.
<system.webServer>
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="en(.*)" />
<action type="Redirect" url="https://www.mycompanyLTD.com{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
There is no need to match a /en URL fragment forcibly. We redirect the request as long as we found that we have a /en URL segment. so does the http/https URL segment.
Feel free to let me know if there is anything I can help with.
After several hours of lecturing Regex I've created this rule and seems to be working (I've tested several scenarios):
^(http|https)://?(www.)mycompanyPLC.com/en?(.*)
and the Redirect URL from IIS is:
https://www.mycompanyLTD.com/{R:3}
Later edit:
The rule in IIS is like this:
<rule name="Replace PLC with LTD and remove /en/" enabled="true" stopProcessing="true">
<match url="(.*?)PLC\.com(?:\/\ben\b)?(.*)" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false" />
<action type="Redirect" url="{R:1}ltd.com{R:2}" />
</rule>
Test urls were this format:
http://webdev.myCompanyplc.com/en/our-experience/retail
{R:1} = http://webdev.myCompany
{R:2} = /our-experience/retail
Regex expression was ok, but redirect still didnt work
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>
I'm trying to write a particular regex pattern for a rewrite rule in IIS and if it matches the pattern to stop processing any more rules.
The Url will look something like this:
somesite.com/somepath/34343.aspx
I need to see if I only have numbers in the section 34343 as I can have
somesite.com/somepath/something343.aspx
I have tried matching the pattern like so:
([0-9]*).aspx$
But this picks up the latter URL and stops processing the rules so later matches aren't run. I need them to run on the later rules and not stop processing.
So if anyone can help, I need some way to check if I only have numbers after the last trailing slash and before the last .(dot)
I have also tried this:
(.)/(.).(.*)
which seems to give me what I want, inasmuch as it gives me grouped matches:
Full Match- somesite.com/somepath/34343.aspx
Group 1- somesite.com/somepath
Group 2- 34343
Group 3- aspx
But I don't know how to use Group 2 to then check that text for only numbers?
Can anyone help please?
Thanks
EDIT
Thanks for the replies, but these two patterns aren't working for me. I plug them into the IIS Url rewrite tool and whilst the rather wonderful test pattern option tells me that they match, they rule just doesn't fire.
<rule name="Ignore id with aspx" enabled="true" stopProcessing="true">
<match url="^(.*\/\d+\..+)$" ignoreCase="true" />
<!--OR-->
<match url="^.*\/\d+\.[^.]+$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
</rule>
Or at least it doesn't stop processing anymore subsequent rules.
Coincidentally, the rule I said I was using ([0-9]*).aspx$ does fire and does stop processing the subsequent rules.
You can make the regex like this:
(.*\/)+(\d*\..+)
This will check if it contains only digits.
Thanks for the help, but I managed to figure out why it wasn't firing the rule and it appears to now be working.
I setup Failed Request Tracing Rules and noticed that the rule had removed the base url http://somesite.com/ from the check. So it was only looking for the last bit of the Url.
As the Url's in question were http://somesite.com/12345.apsx it was easy to then check for just numbers and .aspx in the request.
<rule name="Ignore id with aspx" enabled="true" stopProcessing="true">
<match url="^\d*\..+[aspx]" ignoreCase="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="None" />
</rule>
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/
I am trying to create URL rewrite inbound rule for IIS to return the URL before a specific parameter.
Edited: I should have stated the authProvider is always the last parameter.
Example:
http://localhost/WebAccess/Default.ashx?accessionNumber=009&authProvider=Bypass
I want to trim off &authProvider=Bypass from the end of the URL
I've tried:
.*(?=&authProvider)
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Test" enabled="true">
<match url="(.*)&authProvider.+" />
<action type="Rewrite" url="{R:0}" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
The example that you have shown will match what you want, but not change the URL. The result of your match should have the matched string that you want as the result.
I think that the problem may be that you are trying to replace what you match, but since the forward lookup (?= is not part of the match result) when you do the replace you are ending up with the same string as when you started. As an alternative, assuming that you are aware that this will not be very robust if parameter orders change, you could use:
(.*)&authProvider.+
Then replace with
$1
This will result in:
http://localhost/WebAccess/Default.ashx?accessionNumber=009
Essentially it matches the whole string and replaces it with everything before $auth, which is in group 1 ($1).
Update
With your update, I see that the rewrite rule syntax uses {R:1} so $1 should be {R:1} in my example and in your Rewrite Rule should be {R:1}. See here for an example.