Redirect all subdomains to new domain - iis

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.

Related

URL Rewrite causing redirected you too many times

I am using IIS url rewrite rule to redirect from an IP Address to a domain name with the following rule.
<rule name="IPHit" enabled="true" stopProcessing="false">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="10.32.1.132" />
</conditions>
<action type="Redirect" url="https://daart-qa.sandbox.aimsplatform.com/eds-daas/{R:1}" redirectType="Permanent" appendQueryString="true" />
</rule>
Unfortunately, this results in an infinite loop redirect when I go to https://daart-qa.sandbox.aimsplatform.com/eds-daas.
How can I redirect from the IP address without having an infinite loop on dns entry?
Your rule will redirect 10.32.1.132/eds-daas to https://daart-qa.sandbox.aimsplatform.com/eds-daas/eds-daas. Is that expected behavior?
May I know your only face this infinite loop when you access specific URL or all requests hitted by IP address. What loop URL did you see in web browser developer tool?
Post the symtptom of loop URL would help us find the root cause.
Or you need to redirect request 10.32.1.132/eds-daas to https://daart-qa.sandbox.aimsplatform.com/eds-daas? If so you may need two rules side by side.
<rule name="IPHit" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="10.32.1.132" />
<add input="{REQUEST_URI}" pattern="^/eds-daas(.*)" negate="true" />
</conditions>
<action type="Redirect" url="https://daart-qa.sandbox.aimsplatform.com/eds-daas/{R:1}" redirectType="Permanent" appendQueryString="true" />
</rule>
<rule name="IP-HIT2" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="10.32.1.132" />
<add input="{REQUEST_URI}" pattern="^/eds-daas(.*)" />
</conditions>
<action type="Redirect" url="https://daart-qa.sandbox.aimsplatform.com/eds-daas{C:1}" />
</rule>

Target subdomain and host domain with URL rewrite module

Below I have a rule that works great with subdomains of example.com, but I need to use https://example.com (example.com) now. The {C:1} was always the subdomain, but now that I dont have a subdomain it needs to be "other". Is there a way to set a default if the {C:1} is null or empty to "other" and match on the host name without subdomain.
<rule name="Apple Icon Rewrite" stopProcessing="true">
<match url="^(apple-touch-icon.*?)(?:-precomposed)?(.png)$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(.*).example.com$" />
</conditions>
<action type="Rewrite" url="/assets/sports/{C:1}/icons/{R:1}{R:2}" appendQueryString="false" />
</rule>
You can just add one additional rule, which will catch only exmple.com domain like that:
<rule name="Apple Icon Rewrite" stopProcessing="true">
<match url="^(apple-touch-icon.*?)(?:-precomposed)?(.png)$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Rewrite" url="/assets/sports/other/icons/{R:1}{R:2}" appendQueryString="false" />
</rule>

IIS Redirection 301 of a link for only one domain without affecting the links of other domains

i have two domains which share a similar link for a content that is /games-for-cats. What i want is redirecting the games-for-cats link to another link for FR website for instance but leaving the games-for-cats link untouched for other domain. I am using IIS and asp.net MVC so i tried doing this in the Web.Config but this isn't working. Below is what i tried to do.
<rules>
<rule name="Redirect rule pl for Redirects">
<match url=".*" />
<conditions>
<add input="{Redirects:{REQUEST_URI}}" pattern="(^(preprod\.test-website\.pl)(.*)$)" />
</conditions>
<action type="Redirect" url="{C:3}" appendQueryString="false" redirectType="Permanent" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="Redirects">
<add key="/games-for-cats" value="/link-to-be-redirected" />
</rewriteMap>
</rewriteMaps>
Can someone guide to what the correct rewrite rule is.
Thanks.
You need to add an additional condition to your rule which will trigger your rule for a specific domain only:
<rule name="Redirect rule pl for Redirects">
<match url=".*" />
<conditions>
<add input="{Redirects:{REQUEST_URI}}" pattern="(.+)" />
<add input="{HTTP_HOST}" pattern="^preprod\.test-website\.pl$" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent" />
</rule>

How to exclude Azure slots from IIS Redirect Rule

I have a website hosted by Azure , I have 2 slots main and staging. I wrote a condition for redirecting non-www to www like following:
<rule name="WWW Rewrite" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\." />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
My problem is this rule is applying also for the staging slot, like if I typed https://mydomain-staging.azurewebsites.net it redirect me to https://www.mydomain-staging.azurewebsites.net so I couldn't test my staging slot !.
I need to write a rule to exclude my slots which ends with azurewebsites.net
I followed this answer but it didn't work for me.
I solved it by adding this condition :
<rule name="WWW Rewrite" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\." />
<add input="{HTTP_HOST}" negate="true" pattern="^.*\.azurewebsites\.net$" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
A couple of ways. Instead of a catch-all (.*), you can specify the domains you want to redirect.
The other way is to put a condition where if the host name matches *.azurewebsites.net , ignore

IIS 7 URL Rewrite match for particular URL

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>

Resources