IIS Rewrite rule based on length param - url

I have the following rewrite rule
<rule name="Product short redirect" stopProcessing="true">
<match url="product/([A-Za-z0-9]+)/$" ignoreCase="true" />
<action type="Redirect" redirectType="Permanent" url="product-redirect/?code={R:1}" />
</rule>
However i want it only to match product codes that are longer than 3 characters
<rule name="Product short redirect" stopProcessing="true">
<match url="product/([A-Za-z0-9].{4}+)/$" ignoreCase="true" />
<action type="Redirect" redirectType="Permanent" url="product-redirect/?code={R:1}" />
</rule>
But this only returns a partial match and also the three characters codes still match ??
Sample partial urls will be:
product/u22tfp1/
and
product/xxx/

If you want to only match products that are 4 characters or more, you need to specify the length on the regular expression:
E.G: Want a match of (products/1234/ OR products/12345/ )
<match url="product\/([A-Za-z0-9]{4,100}+)\/$" />
I use a match between 4 and 100 on this example (you can also explicitly avoid a match with 3 characters, but it looks uglier on my opinion)
NOTE:
The problem with the previous regular expression product/([A-Za-z0-9].{4}+)/$ is the dot character . that matches everything, basically you are saying match:
"product/" then
"a single character/digit [a-Z0-9]" then
"anything with a length of 4"(repeat this last statement "+" )

Related

URL Rewrite match question mark

I am trying to match a ? in my url rewrite section in my web config, I am using IIS Express so I can not add the rule via IIS manager.
<rules>
<rule name="LocationDetail" stopProcessing="true">
<match url="^location-detail\?province=([A-Za-z]{2})$" />
<action type="Redirect" url="LocationDetail"/>
</rule>
</rules>
As you can see I am already escaping the ?. Is there something I am missing?
I also modify the url so that the the question mark is not needed to see if the question mark is the reason it was not matching.
<rules>
<rule name="LocationDetail" stopProcessing="true">
<match url="^location-detailprovince=([A-Za-z]{2})$" />
<action type="Redirect" url="LocationDetail"/>
</rule>
</rules>
It was. Once the question mark was removed it matched correctly and I was redirected correctly.

IIS-10 configuration for url rewrite on windows server-2016

I have url abc.com/process/login, so how to check is this "process" word present after.com/ using regular expression url pattern?
I do have scenarios abc.com/image/think/process/depth, abc.com/image/think/process_image/depth, abc.com/process_url/machine/code. Then it shouldn't rewrite as "process" word not present next to .com/
So rewrite the url only if "process" word present next to .com/
example: abc.com/process/login , abc.com/process/signup etc..
Try to use this matching rule:
<rewrite>
<rules>
<rule name="Test">
<match url="abc.com/process/(.*)" />
<action type="Rewrite" url="URL address you rewrite" />
</rule>
</rules>
</rewrite>

IIS URL Rewrite with wild card

I need to change current URL pattern (hotels/{City}) to more SEO friendly one villa-{city} and I need to add a wild card for cities. Any city name should allow by the site and previous URL pattern need to rewrite as new pattern. I tried the below rewrite and its working for redirection but it effects on other URL patterns like "hotels/{name}/{code}" too. How can I overcome this redirection effecting on other patterns?
Can someone help me with this?
I would like to know how can we determine whether we use {R:0} Or {R:1} for conditions and the purpose of adding "^" to patterns
<rule name="HotelToVilla" stopProcessing="true">
<match url="hotels/(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="localhost:23617" />
</conditions>
<action type="Redirect" url="http://localhost:23617/villa-{R:1}" appendQueryString="false" redirectType="Permanent" />
</rule>

IIS Url redirect match pattern

I am redirecting URL in the config file. This is what I am doing:
<rule name="award temp redirect" stopProcessing="true">
<match url="(.*?)/?award.aspx$" />
<action type="Redirect" url="http://www.abc.com.au/award.aspx" redirectType="Temporary" />
</rule>
This is working fine for /award.aspx but I want to add for /awards or /award as well. Currently I have create separate rules for /awards or /award.
Can I add these two into one match. If the Url is /award.aspx or /award or /awards then redirect to http://www.abc.com.au/award.aspx.
match accept regex for url value. Try:
<match url="(.*?)/?awards?(\.aspx)?$" />
This will match award, award.aspx, awards and awards.aspx.
Update to match /test as well:
<match url="(.*?)/?(awards?|test)(\.aspx)?$" />
It is just a regular expression. You can match whatever you want.

IIS URL Rewrite not working with query string

I thought this was pretty straightforward, but it refuses to work. The old URL is
http://www.site.com/?q=node/17
It needs to redirect to http://www.site.com. I don't need to worry about wildcards, this is the only query string parameter I need to worry about. The rule I wrote looks like
<rule name="Node17" patternSyntax="ExactMatch" stopProcessing="true">
<match url="http://www.site.com/?q=node/17" />
<action type="Redirect" url="http://www.site.com" appendQueryString="False" />
</rule>
I can test the pattern inside of IIS and it matches, but when I hit the URL in a browser it doesn't redirect. Any thoughts?
As described in Microsoft's documentation:
It is important to understand how certain parts of the URL string can
be accessed from a rewrite rule.
For an HTTP URL in this form:
http(s)://{host}:{port}/{path}?{querystring}
The {path} is matched against the pattern of the rule. The
{querystring} is available in the server variable called QUERY_STRING
and can be accessed by using a condition within a rule.
Rule conditions allow defining additional logic for rule evaluation...
Rule conditions are evaluated after the rule pattern match is successful.
In the URL you wanted to rewrite as a redirect, your {host} = "www.site.com", {path} = "" and {querystring} = "q=node/17". So the {path} part in the URL you wanted to redirect is actually empty, and the rule you used in your question was matched against it and did not match.
Your solution is indeed valid, so I'll quote it here:
<rule name="Node17" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{QUERY_STRING}" pattern="q=node/17" />
</conditions>
<action type="Redirect" url="http://www.example.com" appendQueryString="False" />
</rule>
Of course I figured it out soon after I posted. This does it, not really sure why the exactmatch wasn't working though.
<rule name="Node17" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{QUERY_STRING}" pattern="q=node/17" />
</conditions>
<action type="Redirect" url="http://www.site.com" appendQueryString="False" />
</rule>

Resources