Redirect subdomain in IIS via web.config file - redirect

Looking to redirect old subdomain back to the primary domain name (remove the subdomain if it's seen) in the web.config file for IIS.
For example,
preview.mysite.com redirects to mysite.com
The rule I've tried is this:
<rule name="Redirect away from Preview" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="preview.mysite.com" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
It stops preview subdomain from loading, but shows error message instead of doing a redirect
This page isn’t working
preview.mysite.com redirected you too many times.
What can I do to correct the redirect rule to reroute any preview subdomain back to the base domain?

Got it fixed. Had to hardcode the redirect url and that fixed the looping issue, now redirects to the main domain:
<rule name="Redirect away from Preview" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="preview.mysite.com" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://example.com" redirectType="Permanent" appendQueryString="false" />
</rule>
Hope this helps someone else. (Note: stackoverflow would not allow me to put mysite for the url so it shows example.com instead)

Related

After write the rule, every page is redirected to the error page

I wrote a rule for redirect non-www to www. Now my site goes to http://www.abcd.com.tr www came and that's what I wanted, but whichever page I click on I get to the error page.
My rule code is below;
<rule name="Add WWW" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www\.)(.*)$" />
</conditions>
<action type="Redirect" url="http://www.{C:0}{PATH_INFO}" redirectType="Permanent" />
</rule>
For example when I click this link http://www.abcd.com.tr/tr-tr/deneme/test/ page redirect to error page. Also when I use site without www, there was no problem, all page was working.
Please see what I do for one of my projects, with this you are always redirected to a https://www URL.
PS: I have updated the pattern to include your domain name, please update the "pattern" setting for your project and delete the unnecessary bits.
<rule name="Redirect all domain bar azure and actual domain" enabled="true">
<match url="(.*)" ignoreCase="true"/>
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.abcd\.com\.tr|abcd\-live\-fe\-staging\.azurewebsites\.net" />
</conditions>
<action type="Redirect" url="https://www.abcd.com.tr/{R:1}" redirectType="Permanent" />
</rule>
This will work:
<rule name="Redirect NONWWW to WWW " stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example.azurewebsites.net$" />
</conditions>
<action type="Redirect" url="https://www.example.azurewebsites.nyc/{R:0}" redirectType="Permanent" />
</rule>

redirection from non-www to www and http to https in IIS

I am trying to redirect from non-www site to www and also from http to https.
Http to https works well, but non-www to www shows a page not found error.
This is my configuration:
<rules>
<clear />
<rule name="non-www to www" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^mydomain.com$" />
</conditions>
<action type="Rewrite" url="https://www.{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" />
</rule>
<rule name="http to https" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTPS}" pattern="OFF" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" />
</rule>
</rules>
What is going on? I have tried several variants for non-www to www, all from sites found in google, but non worked.
Jaime
Copied from the comment with more explanation.
The action should be set to Redirect instead of Rewrite, because you do want search engines to show www links, and then your site must issue 301 redirection to their bots.
Even if you sometimes want Rewrite, the url must be a relative path to the same site. If rewriting to another site (like the one you used above), then ARR is required with proxy mode. Otherwise, IIS naturally returns 404 errors.
However, rewriting does not change URLs in browser address bar, so it also has no effect on search engine bots.
Reference

www and http in kentico - IIS Redirect

I would like to create redirects to force https and for removal of www so all permutations direct to https://mysite.co.uk
This is a multi site Kentico system so I only want to redirect the specific domain in question.
Here's what I'm currently using but it's not working. Any help greatfuly appreciated.
<rewrite>
<rules>
<rule name="mysite https redirect">
<match url="^(mysite\.co.uk|www\.mysite\.co.uk)$" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
You can do this outside of IIS and leave it configured with the site only. If you look in Settings>URLS & SEO (after you select the site you want this configured for, upper left), you can make these changes there without a need to use IIS.
One thing to be sure of is to have your site's domain set as the primary domain you want to use. In your case, mysite.co.uk. Then in the site's domain aliases, add the entry www.mysite.co.uk.
Next, on the root of the site in the Pages app, go to Properties>Security and towards the bottom, select Requires SSL. This will replicate through all pages in the site.
I think you need 2 separate rules. Check this answer here:
<rule name="Remove WWW" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)(ccsportal\.com)" />
</conditions>
<action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent"/>
</rule>

Trouble getting redirects right using web.config for IIS

Ok have a strange redirect issue here. I have 3 domain names so lets call them the following
maindomain.com
aliasdomain.net
aliasdomain.org
We are using Let's Encrypt for https via IIS on a windows 2016 Server.
What we want is for anytime a person types in any of the 3 domains with or the www to all redirect to the domain name www.maindomain.com
Since the let's encrypt certificate is not creating www. versions for the domain aliases it is causing us some struggles. This is the web.config rules we are using but they do not work.
<rule name="Redirect to WWW" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\." negate="true" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}{HTTP_URL}" redirectType="Permanent" appendQueryString="false" />
</rule>
<rule name="Redirect to HTTPS" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="OFF" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{HTTP_URL}" redirectType="Permanent" appendQueryString="false" />
</rule>
So the results of this are the following
This works
www.maindomain.com works and redirects to https://www.maindomain.com
maindomain.com works and redirects to https://www.maindomain.com
This doesn't work and oddly enough shows the standard IIS landing page
aliasdomain.net doesn't work redirects to http://www.aliasdomain.net
www.aliasdomain.net doesn't work and redirects to http://www.aliasdomain.net
This one doesn't work at all
aliasdomain.org doesn't work redirects to https://www.aliasdomain.org
www.aliasdomain.org doesn't work and redirects to https://www.aliasdomain.org
No idea how to get this to do what we want. You would think it would not be some difficult to redirect any version of any domain name to the https://www.maindomain.com
Thanks for any help here.
The problem with your rule is that you are using {HTTP_HOST} in your
redirect action.This parameter will take the incoming hostname from
the request.I think that is not what you want. <action
type="Redirect" url="https://{HTTP_HOST}{HTTP_URL}"
redirectType="Permanent" appendQueryString="false" />
Please try below rule. It redirects if the hostname does not match
www.maindomain.com ,also enforce https
<rule name="CanonicalHostNameRule1" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^www\.maindomain\.com$" negate="true" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://www.maindomain.com/{R:1}" />
</rule>

IIS not redirecting https://domain.com to https://www.domain.com

I have a problem that I can't seem to find any solution to online. All searches are for http to https or non-www to www but without the http in front.
My site will not redirect https://domain.com to https://www.domain.com. It redirects domain.com to www.domain.com just fine though, it's only when the https is already entered does it not redirect and thus gives an error.
I have this for redirecting http to https:
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
</rule>
Edit: after experimenting, it manages to redirect http://domain.com to https://www.domain.com correctly (i.e from without s to with s and www).
Yet still doesn't from https://domain.com to https://www.domain.com
Figured it out, after adding this:
<rule name="Force WWW and SSL" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^[^www]" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://www.domain.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
And most importantly, removing the hostname field 'www.domain.com in IIS > Site Bindings > Edit > Host Name, and unticking 'Require Server Name Indication'. Having my domain entered here was causing the above code to not work. I originally added this thinking it was necessary, but not so.