IIS URLRewrite https non-www to https-www - asp.net

Having a tough time configuring rewrite rules.
I basically want :
Non www requests to be redirected to one with www
Non https requests should be redirected to one with https
Desired Redirects:
Requested Url Redirected To
http://example.com https://www.example.com/
http://www.example.com https://www.example.com/
https://example.com https://www.example.com/
https://www.example.com https://www.example.com/
Rewrite configuration 1:
<rules>
<rule name="Redirect HTTP to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" />
<!--redirect to https -->
</rule>
<rule name="Add WWW prefix" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="https://www.example.com/" />
</rule>
</rules>
Rewrite configuration 2: (from IIS Redirect non-www to www AND http to https).
<rules>
<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.example.com/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
The issue is mainly with https://example.com - it's not directing properly

Try this:
<rules>
<rule name="Redirect example.com to www" patternSyntax="ECMAScript" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent" />
</rule>
<rule name="Redirect to example.com https" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" negate="false" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>

Related

Redirect from www to none-www not working

The app is built on asp.net core 3.1 and react. They are on the same origin.
In web.config redirection from http to https working.
In web.config redirection from www.example.com to example.com not working
// Piece of my web.config
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
</rule>
<rule name="RedirectWwwToNonWww" 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>
</rules>
In browser www.example.com goes in redirection loop and shows
ERR_TOO_MANY_REDIRECTS
In Postman the error is Error: Exceeded maxRedirects. Probably stuck in a redirect loop https://www.example.com/
You can try this rule to redirect from www to none-www:
<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>

Possible to exclude subfolder from https rewrite?

I have a webiste that runs https. Now some programmers need a subfoder that has no https.
This is what i have in my web.config right now:
<rule name="HTTP Redirect to HTTPS" enabled="true" stopProcessing="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>
We need the folder "wp-content\rest" to be excluded from this https redirect.
In hope someone can help me with this one.
You need to add an additional condition into your rule:
<rule name="HTTP Redirect to HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
<add input="{REQUEST_URI}" pattern="^/wp-content/rest" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
Then this rule will disable redirects to all urls like that http://www.example.com/wp-content/rest*

force website http:// to https://www asp.net

I want to redirect non-www and non-https URLs to https://www for my domain, I actually have the following in web.config, that works ok redirecting non-https to https but still allows to access domain.com without www,
<rewrite>
<rules>
<clear />
<rule name="Default to root" stopProcessing="true">
<match url="(.*)default.aspx" />
<action type="Redirect" url="{R:1}" />
</rule>
<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" />
</rule>
</rules>
</rewrite>
I have shared hosting..
This is a running code
<rewrite>
<rules>
<clear />
<rule name="Default to root" stopProcessing="true">
<match url="(.*)default.aspx" />
<action type="Redirect" url="{R:1}" />
</rule>
<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" />
</rule>
<rule name="Add www" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mydomain\.com$" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Thanks #lexLi

IIS 7 URL Rewrite HTTPS to HTTP

I updated my rules and can get every redirect to work except for https://www.freertool.com/store/en links
for example https://www.freertool.com/store/en/freer-naams-alb081 wont work but if i remove the S on HTTPS it goes through. I am just missing a step i believe should i create another rule to handle Https prior to loading the other rules?
<rules>
<clear />
<rule name="Store Redirect Store www" stopProcessing="true">
<match url="^www.freertool.com/store(.*)" />
negate="true" />
<action type="Redirect" url="http://freertool.com{R:1}" />
</rule>
<rule name="Store Redirect EN" enabled="true" stopProcessing="true">
<match url="^en(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="http://freertool.com{R:1}" />
</rule>
<rule name="Store Redirect Store" stopProcessing="true">
<match url="^store(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="http://freertool.com{R:1}" />
</rule>
<rule name="Store Redirect www and en" enabled="true">
<match url="^www.freertool.com/store/en/(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
</conditions>
<action type="Rewrite" url="http://freertool.com{R:1}" />
</rule>
</rules>

IIS7 redirect https to http except 1 page

I have a rule
<rule name="HTTPS to HTTP Redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="on" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
that redirects ALL HTTPS to HTTP however I now need 1 page to be HTTPS.
Thanks
Try this. I have assumed your page that is to force HTTPS is login.aspx.
<rewrite>
<rules>
<rule name="Force HTTPS for 1 page " stopProcessing="true">
<match url="(.*)/login.aspx" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:0}" redirectType="Permanent" />
</rule>
<rule name="Others Force HTTP" stopProcessing="true">
<match url="((.*))" negate="true" />
<conditions>
<add input="{HTTPS}" pattern="^ON$" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
Hi you can try regular expression below: This will match everything with https except for help.html page.
^[hH][Tt]{2}[pP][Ss](?!.*help.html).*$

Resources