IIS 7 URL Rewrite match for particular URL - iis

I would like to match exactly https://internet.dev.local/KYR url (without / into end) and redirect or rewrite to https://internet.dev.local/KYR/ (with /).
I am trying the following rule but it matches other URLs as well e.g. https://internet.dev.local/KYR/Admin/Form/Default.aspx?signup=false, which is wrong.
so how can I achieve this?
<rule name="Static redirect" patternSyntax="ExactMatch" stopProcessing="true">
<match url="https://internet.dev.local/KYR" negate="true" />
<conditions>
</conditions>
<action type="Redirect" url="/Login/?ReturnUrl=/Member/KYR/" redirectType="Permanent" />
</rule>

If you need to test the host and protocol you have to put it in the conditions, not in the global rule.
Following your example, it would be as follow:
<rule name="test" patternSyntax="ExactMatch" stopProcessing="true">
<match url="KYR" />
<action type="Redirect" url="/KYR/" redirectType="Permanent" />
<conditions>
<add input="{HTTP_HOST}" pattern="internet.dev.local" />
<add input="{HTTPS}" pattern="on" />
</conditions>
</rule>
I have changed the url in the action because your question says:
redirect or rewrite to https://internet.dev.local/KYR/ (with /).
EDIT:
To get it to work on any host (with or without SSL), just remove the conditions:
<rule name="test" patternSyntax="ExactMatch" stopProcessing="true">
<match url="KYR" />
<action type="Redirect" url="/KYR/" redirectType="Permanent" />
</rule>

Related

IIS Rewrite Maps with Wildcard for Querystring

Trying to figure out how to do wildcards for querystrings. Right now this works, but it has to be an exact match.
I have this rewrite rule.
<rule name="Redirect rule1 for Redirects ReSv">
<match url=".*" />
<conditions>
<add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="true" />
</rule>
My rewrite maps are like:
<rewriteMaps>
<rewriteMap name="Redirects">
<add key="/contact-us.aspx" value="/contact/" />
<add key="/contact-us/request-information.aspx" value="/contact/" />
</rewriteMap>
</rewriteMaps>
Trying to get something like this:
/contact-us.aspx?q=t&1=2
To redirect to:
/contact/
And include the querystring? so..
/contact/?q=t&1=2
Please us {URL} variable instead of {REQUEST_URI}. Then Rewritemap will be able to wildcard the query string.
<rule name="Redirect rule1 for Redirects ReSv" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{Redirects:{URL}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="true" redirectType="Permanent" />
</rule>

URL Rewrite back reference action URL not working

I have setup a url rewrite rule to redirect from one domain to another in case matching the condition. i have a site abc.aaa.com which should redirect to abc.bbb.com if url matches with *.aaa.com. When I hard code the action URL its working fine but using back reference its not working.
I am using IIS 8.5
Below are the rules.
This is not working. When I am doing this URL is showing http://abc.aaa.com/abc.bbb.com
<rule name="Redirect aaa.com" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="(.*).aaa.com(.*)$" />
</conditions>
<action type="Redirect" url="{C:1}.bbb.com{C:2}" appendQueryString="false" />
</rule>
This is working
<rule name="Redirect aaa.com" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="(.*).aaa.com(.*)$" />
</conditions>
<action type="Redirect" url="http://abc.bbb.com" appendQueryString="false" />
</rule>
I have tried the same pattern in rule pattern and same action without condition. thats also not working
<rule name="Redirect aaa.com" enabled="true" stopProcessing="true">
<match url="(.*).aaa.com(.*)$" />
<action type="Redirect" url="http://abc.bbb.com" appendQueryString="false" />
</rule>
I have found that {C:1} doesn't contain http:// part and seperated that in URL action . it's working fine.Below is the modified rule
Thanks Lex Li for the quick response
<rule name="Redirect aaa.com" enabled="true" stopProcessing="false">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="(.*).aaa.com(.*)$" />
</conditions>
<action type="Redirect" url="http://{C:1}.bbb.com{PATH_INFO}" appendQueryString="true" />
</rule>

IIS URL rewrite rule for https and containing path

I'm trying to figure out a url rewrite rule to rewrite http requests into https only when a specific path is accessed like below. I've tried quite a few things that in the test pattern seem as though they should work but it never does.
url accessed: http://example.com/books/test.css
I need to check for the http:// and /books/ to form the proper url below.
url needed: https://example.com/books/test.css
A request of http://example.com/book/test.css should be ignored.
You can specify the patten in the <match> element :
<rule name="Force HTTPS for /books/" enabled="true">
<match url="^/books/.*$" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" redirectType="Permanent"
url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="true" />
</rule>
or by adding a new <condition>
<rule name="Force HTTPS for /books/" enabled="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
<add input="{REQUEST_URI}" pattern="^/books/.*$" />
</conditions>
<action type="Redirect" redirectType="Permanent"
url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="true" />
</rule>

Url Rewrite Module Not Working When Browsed Too

Why isn't this rule working when I go to a browser with the URL rewrite module?
It works on the regex tester with the url rewrite module.
I even put it at the top of all my rules.
Example url: organizations/51/middle-tennessee-basketball-showcases-basketball-tournaments?page=1
Rewrite rule:
<rule name="Organization Redirect" stopProcessing="true">
<match url="^organizations/(.*)-basketball-tournaments\?page=1$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="organizations/{R:1}" appendQueryString="false" />
</rule>
Your rule should be as following:
<rule name="Organization Redirect" stopProcessing="true">
<match url="^organizations/(.*)-basketball-tournaments$" />
<conditions>
<add input="{QUERY_STRING}" pattern="^page=1$" />
</conditions>
<action type="Redirect" url="organizations/{R:1}" appendQueryString="false" />
</rule>
You should not check the query string (here page=1) in the url test but in the conditions section.

IIS Rewrite Module to map querystrings

I'm using the IIS Rewrite Module on IIS7.5. My mappings is in a text file in the structure:
[old url], [new url]
So something like this works:
products/abc, http://test.com/new/products/abc
This uses the following rule in my web.config
<rule name="FileMapProviderRule" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{FileMapProvider:{R:1}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" redirectType="Permanent" />
</rule>
What will my rule be if i want querystrings to be passed too? So i want this to work:
products?sku=123, http://test.com/new/products/123
products?sku=789, http://test.com/new/products/789
I solved this by the following rule:
<rule name="Products" patternSyntax="Wildcard" stopProcessing="true">
<match url="products" />
<conditions>
<add input="{QUERY_STRING}" pattern="sku=*" />
</conditions>
<action type="Redirect" url="http://test.com/new/products/{C:1}" appendQueryString="false" redirectType="Permanent" />
</rule>

Resources