IIS URL Rewrite for sub domains - iis

I am trying to set up a URL Rewrite in IIS, which will take a request that is coming from the root https://www.test.com redirects to https://www.test.com/nfc/ but, not redirect the request when the subdomain is 'app' (e.g. https://app.test.com).
I've configured the IIS rewrite script with following attributes:
Request URL: Matches the Pattern
Using: Regular Expression
Pattern: ^$
Condition: {HTTP_HOST} Does Not Match the pattern ^app.test.com$
Action Type: Redirect
Redirect URL: /nfc/
Redirect Type: 307
Will there be a possibility of a recursive redirect that crashes the request.
Here are the IIS settings

There is a problem with your rule, you can try this rule:
<system.webServer>
<rewrite>
<rules>
<rule name="test" stopProcessing="true">
<match url="^$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^app.test.com$" negate="true"/>
</conditions>
<action type="Redirect" url="/nfc" />
</rule>
</rules>
</rewrite>
</system.webServer>

Related

Redirect All Traffic in IIS 10 to another site

I am using URL rewrite to try and set up a rewrite from one site to the another site. I used URL re-write to set up this rule:
<httpRedirect enabled="false" destination="https://www.previdence.com" exactDestination="false" />
<rewrite>
<rules>
<rule name="SiteRedirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://www.previdence.com" appendQueryString="false" />
</rule>
</rules>
</rewrite>
I have followed a couple of tutorials on setting this up and this is what should be set. So if I type in companyname.net it will redirect to https://www.companyname.com. If I type in https://www.companyname.net or https://companyname.net it goes to a 404.
Looking at the bindings for the old site there are bindings for PORT 443 and PORT 80 for www.companyname.net. I set the redirect in IIS for code 302, then I got URL rewrite as I explained above and I still get a 404 error.

URL Rewrite giving 404

I have subdomain with an A record to my IIS Server:
sub.domain.com
The IIS server has a binding for sub.domain.com and a cert for *.domain.com
What we need is when someone goes to sub.domain.com it takes them to another site but masks the URL and keeps the SSL. The destination page has in its SAN cert sub.domain.com.
Tried a forward on Godaddy which works but it doesn't keep the SSL and we get cert issues. So thought was to point to our server to pass SSL and then redirect it. I tried a URL Rewrite but it's giving a 404.
URL Rewrite:
<rewrite>
<rules>
<clear />
<rule name="Pay Redirect" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(www\.)?sub\.domain\.com$" />
</conditions>
<action type="Redirect" url="https://www.domain2.com/cgsdesktop/PaymentLanding/UniversalPortal" appendQueryString="false" />
</rule>
</rules>
</rewrite>
I just need to know how to get this done.
How about this:
<rules>
<rule name="Pay Redirect" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^sub.domain.com$" ignoreCase="true"/>
<add input="{HTTP_HOST}" pattern="^www.sub.domain.com$" ignoreCase="true"/>
</conditions>
<action type="Redirect" url="https://www.domain2.com/cgsdesktop/PaymentLanding/UniversalPortal" appendQueryString="false" />
</rule>
</rules>
use sub.domain.com instead of sub\.domain\.com.
This article contains some useful recipes on IIS redirect/rewrite rules.
In order to mask the URL and keep the SSL in the browser bar, we need to create URL Rewrite action rules of URL Rewrite extension.
However, URL Rewrite action only supports to redirect the request to the same domain by default. We have to install the Application Request Routing extension when redirecting the request to another website.
https://www.iis.net/downloads/microsoft/application-request-routing
or we will get an Http 404 error.
See my preceding post for more details.
ASP.net URL Rewrite subdirectory to external URL
Feel free to let me know if there is anything I can help with.

IIS Url Rewrite to another server

I have an old url www.mydomain.com/customer/1. I changed that server to ww2 and it displays fine at ww2.mydomain.com/customer/1. On my new IIS 8.5 www server how can I put in a rewrite rule so if the user goes to www.mydomain.com/customer/1 they will redirect to ww2.mydomain.com/customer/1. I have tried numerous patterns in the Url Rewrite 2.0 module and nothing seems to work. All other www requests I do not want redirected, those stay on my new www server.
I you want to redirect a single path, /customer/1, then you should add this to Web.config on the www.mydomain.com server:
<rewrite>
<rules>
<rule name="Redirect 'www.mydomain.com/customer/1' to 'ww2.mydomain.com/customer/1'" stopProcessing="true">
<match url="^customer/1$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.mydomain\.com$" />
</conditions>
<action type="Redirect" url="http://ww2.mydomain.com{REQUEST_URI}" appendQueryString="false" />
</rule>
</rules>
</rewrite>
If you wish to include additional paths, you can fiddle with the RegEx specified in the <match url="^customer/1$" /> tag.

IIS URL Rewrite - add query string var depending on domain

Using the IIS url rewrite module for IIS 7, is it possible to accomplish the following:
I have 2 domains, domain1.com and domain2.com. Both domains represent two different but very closely linked events for our company. So I would like the users to manage the site data from the same CMS.
So basically:
domain1.com and domain2.com refer to the same index.php and depending on the QS parameters, the user is presented the content for the respective event. So my index.php would take the following qs:
index.php?site=event1&lang=en&route=home
What i would like would be that:
domain1.com/en/home
would refer to:
index.php?site=event1&lang=en&route=home
and
domain2.com/en/home
would refer to:
index.php?site=event2&lang=en&route=home
It would be like iis url rewrite would append the site parameter with a value based on the domain.
is this possile?
Try adding this IIS Rewrite rule to your web.config:
<rule name="domain1.com" stopProcessing="true">
<match url="^en/home" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(domain1.com|www.domain1.com)$" />
</conditions>
<action type="Rewrite" url="http://www.domain1.com/index.php?site=event1&lang=en&route=home" logRewrittenUrl="true" />
</rule>
<rule name="domain2.com" stopProcessing="true">
<match url="^en/home" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(domain2.com|www.domain2.com)$" />
</conditions>
<action type="Rewrite" url="http://www.domain2.com/index.php?site=event2&lang=en&route=home" logRewrittenUrl="true" />
</rule>
These rules basically say:
Match any request for /en/home
For the domain1.com or www.domain1.com
Rewrite the user to the correct site/event

Remove specific querystrings from the location Url of a redirect response using ISAPI and IIS

I have a few querystring keys ("mobile", "nomobile", etc.) that I do not want to be sent in any 301 redirect responses.
For example, let's say I have the Url /about-us that redirects to /about.
RewriteRule ^about-us$ /about [NC,L,R=301]
Rewrite rules by default keep querystings in the redirect Url. So for an incoming Url like this:
/about?mobile=true&xyz=1
If a redirect rule is applied, I want the server to respond with a location Url that has the mobile querystring removed from the redirect Url, but still containing the xyz querystring. So I want this request to return with this destination Url:
/about?xyz=1
I don't want the (mobile, nomobile, etc.) querystrings removed from the incoming request. If the Url results in a 200, I want the underlying ASP.NET Web applications to see the mobile querystring. This querystring removal should happen on the Location header (i.e. destination Url) of the redirect response.
I have thousands of ISAPI RewriteRules, so I don't want to apply a RewriteCond to every rule.
Is there an ISAPI rule or a custom module I can put somewhere to apply this logic globally to ISAPI generated redirects or to any redirect responses coming out of IIS? Thanks for your help.
Using Url Rewrite in IIS, you can create rules to modify outbound response headers. Below are the rules generated from the Url Rewrite tool:
<system.webServer>
<rewrite>
<outboundRules>
<clear />
<rule name="Remove nomobile from location">
<match serverVariable="RESPONSE_Location" pattern="^(.*)\?nomobile(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
<action type="Rewrite" value="{R:1}?{R:2}" />
</rule>
<rule name="Remove mobile=true from location">
<match serverVariable="RESPONSE_Location" pattern="^(.*)\?mobile(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
<action type="Rewrite" value="{R:1}?{R:2}" />
</rule>
<rule name="Replace &">
<match serverVariable="RESPONSE_Location" pattern="^(.*)(\?&)(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
<action type="Rewrite" value="{R:1}?{R:3}" />
</rule>
<rule name="Remove empty ?" enabled="true">
<match serverVariable="RESPONSE_Location" pattern="(.*)\?$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true" />
<action type="Rewrite" value="{R:1}" />
</rule>
</outboundRules>
</rewrite>
</system.webServer>

Resources