I am trying to write a rule to route the website to specific URL based on certain condition.
For example, my web site is http://www.sportStore.com/site/department/123. What I want is if I type http://www.sportStore.com/site/department/999 then it should route to https://www.sportStore.com/items/. How to do that? I tried following which did not work.
Code:
<rule name="Site Redirect" enabled="true" stopProcessing="true">
<match url="site/department/(.*)" />
<action type="Redirect" url="https://www.sportStore.com/items/" appendQueryString="false" redirectType="Permanent" />
<conditions logicalGrouping="MatchAny">
<add input="{QUERY_STRING}" pattern="((123)|(456)|(789)|(111)|(222)|(333))" negate="true" />
</conditions>
</rule>
Actual Results:
http://www.sportStore.com/site/department/123 -> https://www.sportStore.com/items/
http://www.sportStore.com/site/department/456 -> https://www.sportStore.com/items/
http://www.sportStore.com/site/department/999 -> https://www.sportStore.com/items/
Expected Results:
http://www.sportStore.com/site/department/123 -> http://www.sportStore.com/site/department/123
http://www.sportStore.com/site/department/456 -> http://www.sportStore.com/site/department/456
http://www.sportStore.com/site/department/999 -> https://www.sportStore.com/items/
Try the following rule for only permanently redirecting 999 department. You can obviousily add other department ids in the match url pattern:
<rule name="Site Redirect" enabled="true" stopProcessing="true">
<match url="site/department/((999))" />
<action type="Redirect" url="https://www.sportStore.com/items/" appendQueryString="false" redirectType="Permanent" />
</rule>
Related
I want to redirect specific URLs permanently to other domains.
<rule name="URL Test" enabled="true" stopProcessing="true">
<match url="^en/career/jobs$" />
<conditions logicalGrouping="MatchAny">
</conditions>
<action type="Redirect" url="https://www.example.com/newjobs" appendQueryString="false" redirectType="Permanent" />
</rule>
It works when I use https://source.com/en/career/jobs
but when I add a slash in the end (https://source.com/en/career/jobs/), it doesnt work.
How can I accept both variants in the match url ?
You just need to change your parameters to match both en/career/jobs/ and en/career/jobs. You can use the following rules as a reference:
<rule name="URL Test" enabled="true" stopProcessing="true">
<match url="^en/career/jobs(.*)$" />
<action type="Redirect" url="https://www.example.com/newjobs" appendQueryString="false" redirectType="Permanent" />
</rule>
I want to redirect (301) all traffic to a new domain using a web.config rule, but the site is also handling a growing list of subdomains which will also need redirection.
How do I write a rule for the following?
The old domain is foo.com
The new domain is bar.org
Everything before and after the domain (foo.com) needs to be redirected to the new domain:
a.foo.com -> a.bar.org
b.foo.com -> b.bar.org
c.foo.com/some-page -> c.bar.org/some-page
d.foo.com/some/other/page -> d.bar.org/some-page
Basically like this but for IIS.
This is my attempt so far:
<rule name="redirect" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^(.*)\.foo\.com$" />
</conditions>
<action type="Redirect" url="http://{C:1}.bar.org/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
Ahh, I finally got it working:
<rule name="redirect" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="false" pattern="^(.*)\.foo\.com" />
</conditions>
<action type="Redirect" url="https://{C:1}.bar.org/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
I don't fully understand the rule though :/ It doesn't work without setting "negate" to false for example.
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>
I try to make a permanent redirection on an azure website using the rewriting module using this pattern :
<rule name="Rewrite redirect-not-found-products-w" patternSyntax="ExactMatch" stopProcessing="true">
<match url="product/Product.aspx?product_id=287"/>
<action type="Redirect" url="https://example.com/product" redirectType="Permanent"/>
</rule>
But I would like take care of GET parameters
By example redirect :
example.com/product/Product.aspx?product_id=287
or
example.com/product/Product.aspx?product_id=35
to
example.com/product
but not the whole product/Product.aspx
Your rule should have condition with query string, you cannot use query string in match url=
<rule name="Rewrite redirect-not-found-products-w" stopProcessing="true">
<match url="^product/Product.aspx$" />
<conditions logicalGrouping="MatchAny">
<add input="{QUERY_STRING}" pattern="^product_id=287$" />
<add input="{QUERY_STRING}" pattern="^product_id=35$" />
</conditions>
<action type="Redirect" url="https://example.com/product" redirectType="Permanent" />
</rule>
Rule above will redirect:
example.com/product/Product.aspx?product_id=287
or
example.com/product/Product.aspx?product_id=35
to
example.com/product
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>