IIS Url redirect match pattern - url

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.

Related

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 multiple parameters

I am really confused about the URL rewrite interface. I dont understand what i need to do.
I have an url as:
www.example.com/diretory/subdirectory/index.html?param1=1&param2=2&param3=2&param4=7
I want to hide this url in a <a>-href tag, which displays "Example Tag".
When the url is requested it should rewrite it to
www.example.com/program/location/year/vacancie
I allready tried this:
<system.webServer>
<rewrite>
<rules>
<rule name="ProgramRewrite" patternSyntax="ECMAScript" stopProcessing="true">
<match url="\?([^/]+)&([^/]+)&([^/]+)&([^/]+)" />
<action type="Rewrite" url="www.example.com/program/location/year/vacancie" logRewrittenUrl="true" />
<conditions>
</conditions>
</rule>
</rules>
</rewrite>
</system.webServer>
In the URL Rewrite Interface the Test Pattern said it is working and gets:
?param1=1&param2=2&param3=2&param4=7
param1=1
param2=2
param3=2
param4=7
I checked the log url rewrite as well, but in my logs it is not shown.
2017-03-20 16:29:24 192.168.253.146 GET /diretory/subdirectory/index.html param1=1&param2=2&param3=2&param4=7 88 - 192.168.253.146 Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/56.0.2924.87+Safari/537.36 - 304 0 0 4
ps: the urls are not working and only for illustrative purpose.
The match URL only matches the URL and does not take the querystring into account. You will need to add a condition for this. Also do you want to rewrite this (so the server internally executes the new URL) or redirect (so the server will request the browser to go to the new URL and URL changes in address bar). In case you want to rewrite you should not add the domain again, in case you want to redirect add http:// as well. Assuming a rewrite is what you want use below rule:
<rule name="ProgramRewrite" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="/program/location/year/vacancie" logRewrittenUrl="true" />
<conditions>
<add input="{QUERY_STRING}" pattern="([^/]+)&([^/]+)&([^/]+)&([^/]+)" />
</conditions>
</rule>

IIS Rewrite rule based on length param

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 "+" )

Using URL rewrite with sub domains; download file fails

I currently have this rewrite URL rule in my web.config file
<rule name="Rewrite to qa" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^qa.golfgameskeeper.com$" />
</conditions>
<action type="Rewrite" url="qa/{R:1}" />
</rule>
it works great, makes qa.golfgameskeeper.com/qa work like qa.golfgameskeeper.com
However, when I try to download a file from a sub-directory of qa it seems to get confused
http://qa.golfgameskeeper.com/apps/iOS will list the file, but will not allow me to download it.
Even when clicking one the link above the rule re-writes the link to
qa.golfgameskeeper.com/qa/apps/iOS (cut and paste will work, not clicking the link)
Is there a way to modify this rule to allow what I'm trying to do? So, as I'm writing this I figured out what I am trying to do.
have
qa.golfgameskeeper.com -> qa.golfgameskeeper.com/qa (works)
and allow
qa.golfgameskeeper.com/apps/iOS to download the file without rewriting the URL twice (which is what I think it does).
Thank you,
You can add a rule before your rewrite rule to match on the apps subdirectory and do nothing, basically skipping the rewrite rule.
<rule name="Skip apps" stopProcessing="true">
<match url="^apps/(.*)" />
<action type="None" />
</rule>
<rule>
//your rewrite rule here
</rule>

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