Web.config URL redirect to non-www + https - redirect

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/

Related

redirect some domains to https and some to http in IIS

I redirect all requests to https with below rule but now I want to prevent redirection for some subdomains.
I redirected test.com to https://test.com, sub.test.com to https://sub.test.com
But I don't want sub2.sub.test.com to be redirected.
<rule name="https2" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="Temporary" />
</rule>
What should I do?
Thank you
You could try below rule:
<rule name="http2" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^test.com$|^sub.test.com$" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
this rule only redirects domain wich set in condition.

Redirect to https://www in web.config

I'd like both example.com and www.example.com to be redirected to https://www.example.com
At the moment, I have these two rules in my web.config:
<rule name="HTTP/S to HTTPS Redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{SERVER_PORT_SECURE}" pattern="^0$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
<rule name="Root Redirect to www" stopProcessing="true">
<match url=".*" negate="false" />
<action type="Redirect" url="{C:1}://www.{HTTP_HOST}/{R:0}" />
<conditions trackAllCaptures="true">
<add input="{CACHE_URL}" pattern="^(.*)://" />
<add input="{HTTP_HOST}" pattern="^(?!www\.).*" />
</conditions>
</rule>
Is it possible to combine them, so the user only experiences one redirect?
Update:
This seems to do the trick:
<rules>
<clear />
<rule name="Redirect non-www OR non-https to https://www">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^example.com$" />
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent"/>
</rule>
</rules>
Any downsides?

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

IIS7.5 redirecting non www to www also with https

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>

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/