Trouble getting redirects right using web.config for IIS - redirect

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>

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>

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>

https://www.subdomain.domain.com to https://subdomain.domain.com redirect

My boss wants to ensure that our internet-challenged users will be able to reach a particular subdomain (https://subdomain.domain.com) by typing it into their browser's address bar as:
https://www.subdomain.domain.com
It seems I am able to do the HTTP -> HTTPS redirect without issue using IIS's Rewrite module. Also it seems I am able to do the WWW.subdomain.domain.com to subdomain.domain.com with the rewrite module. However, combining them is proving difficult. So, it's when the user types in their browser https://www.subdomain.domain.com that ultimately fails with a privacy warning (our wildcard cert is for *.domain.com).
I've got a CNAME record set up pointing www.subdomain.domain.com to the A record for subdomain.domain.com. I've got the bindings in IIS set up for each of the 4 combinations (with/without https, with/without www).
I have the following in the config for the site:
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Remove WWW" patternSyntax="Wildcard" stopProcessing="false">
<match url="*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{CACHE_URL}" pattern="*://www.*" />
</conditions>
<action type="Redirect" url="{C:1}://{C:2}" redirectType="Permanent" />
</rule>
<rule name="http to https" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther" />
</rule>
</rules>
</rewrite>
</system.webServer>
Still it seems that I can't get the browser to like the redirect from the https://www.subdomain.domain.com to https://subdomain.domain.com. Why isn't the rewrite taking care of this before the browser attempts to serve www.subdomain.domain.com. Is this even possible??

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.

Simple rule(s) for IIS/Azure web.config file to redirect traffic to non-www HTTPS

My goal is to have rule(s) in IIS/Azure's web.config file to successfully redirect the following client request examples:
HTTPS redirect:
From: example.com
To: https://example.com
No-www & HTTPS redirect:
From: www.example.com
To: https://example.com
No-www, HTTPS redirect & maintain path:
From: www.example.com/examplepage
To: https://example.com/examplepage
Here's what I have so far, which only is achieving point 1 and 2. If a visitor lands directly on a subpage, they aren't redirected to non-www and aren't redirected to HTTPS.
<rule name="Redirect to https">
<match url="(.*)"/>
<conditions>
<add input="{HTTPS}" pattern="Off"/>
<add input="{REQUEST_METHOD}" pattern="^get$|^head$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}"/>
</rule>
<rule name="Canonical Hostname" stopProcessing="false">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:2}{REQUEST_URI}" redirectType="Permanent" />
</rule>
I've exhausted multiple search result pages and multiple StackOverflow questions without any luck - they all promise to be able to achieve the third point (redirect to a non-www, HTTPS page and maintain the path) but they haven't worked for me.
Thanks for taking the time to read this!
I discovered the problem, which is partly due to the way I presented my question (sorry, StackOverflowers!).
The issue was the ordering of the rules. I've put the following (new) rules at the top of my rules section, in the following order, for those who suffer this problem in the future. Now, it is redirecting to HTTPS, with no-www, and maintaining the path that was requested by the client.
<rule name="Canonical Hostname" stopProcessing="false">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="http://{C:2}{REQUEST_URI}" redirectType="Permanent" />
</rule>
<rule name="Force HTTPS" enabled="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>