force website http:// to https://www asp.net - 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

Related

Redirect Http to Https in web.config Return not found

I want to redirect this club.mysite.com to https://club.mysite.com.
i wrote this code in web.config:
<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>
i used IIS10.any solution ?

IIS URLRewrite https non-www to https-www

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>

Redirection from "https://www" to "https://"

We have the ASP.NET application hosted as Azure WebApp and configured the domain from GoDaddy. We bought SSL for non-www domain. We used the following redirection rule:
<rewrite>
<rules>
<rule name="Redirect to Example.com">
<match url="(.*)"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!^Example.com)" />
</conditions>
<action type="Redirect" url="https://Example.com/{R:1}"/>
</rule>
<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>
</rules>
</rewrite>
Is there a way to add another rule to redirect request to "https://www" to "https://"?
Thanks,
Jay
I had a similar challenge with my Windows hosting since I have a wildcard cert and host a few subdomains. Here's the web.config I put together, give it a shot and let me know! It seems to work great for me, but my site is extremely simple.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear />
<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>
<rule name="remove www" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.YourDomain\.com$" />
</conditions>
<action type="Redirect" url="https://YourDomain.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

url rewrite - error This webpage has a redirect loop

I would like to redirect all traffice coming from http://www.example.com to http://www.mysite.com/badreferer.aspx?bad=true
for this i tried to create a rule in web.config file of http://www.mysite.com in IIS7.
<rule name="bad referer" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_REFERER}" pattern="(.*)example(.*)" />
</conditions>
<action type="Redirect" url="/badreferer.aspx?bad=true" appendQueryString="false" />
</rule>
But having issue with redirect loop.
Please help.
Thanks.
Try this:
<rules>
<rule name="bad referer" stopProcessing="true">
<match url="^(.*)" />
<conditions>
<add input="{HTTP_REFERER}" pattern="http://www.example.com(.*)" negate="true" />
</conditions>
<action type="Redirect" url="http://www.website.com/badreferer.aspx?bad=true" />
</rule>
</rules>
update:
<rule name="bad referer" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{REQUEST_URI}" pattern="/badreferer\.aspx?bad=true" negate="true" />
<add input="{HTTP_REFERER}" pattern="^www.\example\.com.*" />
</conditions>
<action type="Redirect" url="/badreferer.aspx?bad=true" appendQueryString="false" />
</rule>

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