IIS7.5 redirecting non www to www also with https - redirect

I am trying to configure my IIS to redirect all none www calls to www calls including https
That first part is quite easy, but i have not found a solution that will also work when you have https enabled on your bindings.
So I would like both
http://domain.com/
and
https://domain.com/
and
http://www.domain.com/
to all be redirected to :
https://www.domain.com/
any suggestions?

#Josh: Your answer works for the first request.
It will end up with "https://www", perfect.
But if i then remove "www." from the address, it will fail like the image below.

<rewrite>
<rules>
<rule name="Non www HTTP to HTTPS" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" />
<add input="{HTTP_HOST}" pattern="^www" negate="true" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}{REQUEST_URI}" />
</rule>
<rule name="Non www HTTPS to www HTTPS" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="On" />
<add input="{HTTP_HOST}" pattern="^www" negate="true" />
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}{REQUEST_URI}" />
</rule>
<rule name="HTTP to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" />
<add input="{HTTP_HOST}" pattern="www*" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
</rules>
</rewrite>

This ended up working for me:
<rewrite>
<rules>
<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>
</rules>
</rewrite>

Related

Web.config URL redirect to non-www + https

Currently, my rule is:
<rule name="SecureRedirect" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTPS}" pattern="off" />
<add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:2}" redirectType="Permanent" />
</rule>
The problem is here:
http://www.domainName.com/image.png redirects wrongly to https://domainName.com
instead of https://domainName.com/image.png
and
https://www.domainName.com/image.png doesn't ever redirect to https://domainName.com/image.png
So, what's the true way to redirect all to non-www https URL?
The correct rule, which will fit all your requirements is:
<rule name="SecureRedirect" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
</rule>
Try this rule:
<rule name="SecureRedirect" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>
The way I did it on my site is as follows:
ServerName www.example.com
ServerAlias example.com
Redirect / https://www.example.com/

IIS url redirect from non-www to www not working

I have this rewriting rule:
<rewrite>
<rules>
<rule name="Force non-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.yourdomainname.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
It successfully redirects example.com to https://example.com, however, I need redirection to https://www.example.com, how can I do this?
try this ,just limit the domain or you want dynamic domain?
<rewrite>
<rules>
<rule name="Force non-WWW and SSL" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{HTTP_HOST}" negate="true" pattern="^www.(.*)$" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
use the match all and the negate to redirect

Rewrite Rules for HTTP to HTTPS Redirect

I am trying to redirect all http://thesite.com, http://www.thesite.com, https://thesite.com to https://www.thesite.com. But I don't have the pattern correct. What am I doing wrong?
<rewrite>
<rules>
<rule name="Redirect to HTTPs" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="OFF" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
I found the solution:
<rule name="Redirect non-www OR non-https to https://www">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^thesite.com$" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://www.thesite.com/{R:0}" redirectType="Permanent"/>
</rule>

web.config redirect multiple domains to one AND redirect HTTP to HTTPs

I am using web.config to redirect all HTTP traffic to a HTTPs website
<rule name="Redirect to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
This works just fine.
My client wants me to now redirect a whole load of other non HTTPs .com domains to this HTTPs .co.uk domain.
I found a redirect script:
<rule name="Redirect to www.MYDOMAIN.co.uk" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^(www.)?MYOTHERDOMAIN1.(com|org)$" />
<add input="{HTTP_HOST}" pattern="^(www.)?MYOTHERDOMAIN2.(com|net|org)$" />
</conditions>
<action type="Redirect" url="http://www.MYDOMAIN.co.uk/{R:0}" />
</rule>
For the life of me I cannot seem to combine these script into one script that will pick any of his domains and cleanly point them in the direction of the HTTPs .co.uk domain.
Any ideas would be greatly appreciated as regular expressions are not my strong point.
Kind Regards
You can simply add two rules in the order they must process (with only the last rule set to stopProcessing).
<rules>
<clear />
<rule name="domain redirect" stopProcessing="false">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?EXAMPLE2.(com|net)$" />
</conditions>
<action type="Redirect" url="http://www.EXAMPLE.net{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
</rules>

Web.config redirect one domain to non-https folder and another domain to https folder

I have a domain alias for my site. I would like to know how to redirect requests for domainA.ext to https://domainA.ext/folderA and requests for domainB.ext to http://domainB.ext/folderB
Presently I have the following rule to redirect all http requests to https but it redirects ALL requests to https:
<rule name="Redirect to https" stopProcessing="true">
<match url="(.mydomain.ext*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://mydomain.ext}" redirectType="Permanent" />*
</rule>
It is Windows server 2008, but my cms is in PHP.
I can't think of something more simple than 4 different rules.
The 2 first ones for domainA.ext:
<rule name="Check path folderA" stopProcessing="true">
<match url="^folderA" negate="true" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="domainA\.ext$" />
</conditions>
<action type="Redirect" url="https://domainA.ext/folderA/" />
</rule>
<rule name="Check SSL for domainA" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="domainA\.ext$" />
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://domainA.ext/folderA/" />
</rule>
1st rule: if the path doesn't start with folderA, then it redirects to https://domainA.ext/folderA/
2nd rule: if HTTPS is off, it redirects to https://domainA.ext/folderA/
And the 2 next ones for domainB.ext:
<rule name="Check path folderB" stopProcessing="true">
<match url="^folderB" negate="true" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="domainB\.ext$" />
</conditions>
<action type="Redirect" url="http://domainB.ext/folderB/" />
</rule>
<rule name="Check no SSL for domainB" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="domainB\.ext$" />
<add input="{HTTPS}" pattern="^ON$" />
</conditions>
<action type="Redirect" url="http://domainB.ext/folderB/" />
</rule>
1st rule: if the path doesn't start with folderB, then it redirects to http://domainB.ext/folderB/
2nd rule: if HTTPS is on, it redirects to http://domainB.ext/folderB/