HTTP to HTTPS Redirect in IIS - asp.net

I have to redirect my site from http to https, whenever any user open my site using http.
For example: -
http:\\abc.mywebsite.com should go to https:\\abc.mywebsite.com
Notice that in above URL, it is not www.mywebsite.com, instead it is custom URL as abc.mywebsite.com.
I have tried URL Rewrite tool and followed all steps mentioned here. However, I cannot get URL Rewrite to work properly to redirect.
Here is the URL Redirect rule looks like in IIS: -
Here is how my web.config looks like after adding rule using URL Rewrite.
<configuration>
.....
.....
.....
<system.webServer>
.....
.....
.....
<rewrite>
<rules>
<rule name="Http to Https" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
.....
.....
.....
</configuration>
I have also uncheck Require SSL check box under SSL Settings.
However, after doing all this, my website is still not redirecting to https. Just giving error "...can't reach this page".
Please suggest if I am missing anything here.

I have some doubts about the rewrite rules because I see many examples that are different.
Trying different values like the one from this Microsoft Blog could yield better results:
<rewrite>
<rules>
<rule name="Redirect to http" 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="Found" />
</rule>
</rules>
</rewrite>
You can also setup Tracing for Failed Requests in IIS 7. This could give you insight into why it's failing to redirect.

I was facing same issue within the same site when trying to redirect from http to https.
I did created new site and added the binding as http://www.example.com/
And then I have created new Http Redirect and added my https://www.example.com/ it worked.

Make sure that you have setup the bindings correctly in IIS, the second line highlighted in red should be there.
Type = HTTPS
Host Name = the site address like "site.domain.com"
Port = 443
IP Address = The IP Address of the server you are trying to access.

Related

HTTPS redirect to a folder not working

I just installed https certificate and am having a hard time setting up redirect.
I tried using HTTP Redirect in IIS (7.5) but can't get it to work in IE and in FF and Chrome once I pass the initial login page, I get "Too many redirect" error.
I also tried URL Rewrite but it was total failure so i reverted to HTTP Redirect.
Users used to access the site using "http://example.com/internal". Now, i want to redirect this to "https://internal.example.com/internal" and can't get it to work. I would much prefer not to use the last "internal" in the URL, if possible (the Common Name I used when requesting CSR was internal.example.com).
In HTTP redirect, I set "Redirect requests to this destination" to "https://internal.example.com/internal/default.aspx" and set "Redirect behavior" to "Only redirect requests to content in this directory" with Status Code 301 (Permanent).
Web site has an "internal" folder that contains the .Net application files and requires form authentication. The files in root folder are open to all (i.e. www.example.com is open to all).
Any help you can provide setting this up is greatly appreciated.
Update
I updated web.config and added some URL Rewrite rule but still not quite there.
In IE, when I type example.com/internal it goes to https://internal.example.com but not http://internal.example.com/internal.
In Chrome, it doesn't go to https, stays in http and shows "Not secure".
<rewrite>
<rules>
<rule name="Rewrite to internal" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.example.com$" />
</conditions>
<action type="Redirect" url="{R:1}" />
</rule>
<rule name="internal.example.com to sub folder internal" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_HOST}" pattern="^internal\.example\.com$" ignoreCase="false" />
<add input="{PATH_INFO}" pattern="^/internal($|/)" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/" />
</rule>
</rules>
</rewrite>
The first rule is to just let http://example.com go through; the second is to redirect http://example.com/internal to https://internal.example.com/internal.
Check your Authentication applet in IIS. it should allow anonymous auth.

how to redirect from http to https if we explicitly give http in the url

I want to redirect an url from http to https if we explicitely give http in the url bar.
// I tried keeping the below code snippet in web.config
<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="Permanent" url="some url" />
</rule>
</rules>
</rewrite>
This is working fine when I tried like this in the url bar, for
example: sampledomain.com,
but this is not working when:
I explicitly give http://sampledomain.com in the url bar
I try to access directly home page of our site i.e sampledomain.com/Home
I want the solution for the above two methods.
Hope this link helps:
HTTP to HTTPS redirects on IIS 7.x and higher

301 redirects after domain name change

I run a small ASP.NET4 website on IIS. I recently changed the domain name from 'olddomain.com' to 'newdomain.com'. The change was made through Plesk. After making the change I added 'olddomain.com' as a domain alias in plesk so that traffic would still route through to the website on the old domain.
This is fine so far. The website resolves on both old and new URLs.
However, now I would like to set up 301 redirects everywhere so that any request for olddomain.com are forwarded to newdomain.com instead. None of the website pages have changed - it's just a simple change of domain name.
I've added this to the web.config file:
<rewrite>
<rules>
<rule name="Redirect all to different domain" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^olddomain.com$" />
</conditions>
<action type="Redirect" url="http://www.newdomain.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
But it doesn't seem to have any impact; when I load olddomain.com in the browser it just loads the website without redirecting to newdomain.com.
Can anyone suggest what I've done wrong here and how to get the 301 to work?
Thanks
This works:
<rule name="redirectDomain" stopProcessing="true">
<match url="(.*)" />
<action type="Redirect" url="http://www.newdomain.com/{R:1}" redirectType="Permanent" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^(www.)?olddomain\.com$" />
</conditions>
</rule>
You can set 301 permanent redirect from IIS also.
If http redirection is enabled on old server then you have to put new web config with the contents -
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<httpRedirect enabled="true" destination="http://mynewsite.com/" httpResponseStatus="Permanent" />
</system.webServer>
</configuration>
Let me know if it helps.

HTTP Error 404.4 - Not Found The resource you are looking for does not have a handler associated with it

I've hosted a site in IIS but whenever I browse to the site I get 404.4. How can I solve this? I've referred several posts and they all say the issue is about staticfile but it is already mapped. What more can I do? Here is the attached picture of handler mappings in my iis 7.0
Any ideas?
EDIT:
I have this url rewriter set up:
<rules>
<rule name="Imported Rule 1-1" enabled="true" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{SERVER_PORT}" pattern="80" />
</conditions>
<action type="Rewrite" url="https://abc.com/{R:1}" />
</rule>
When I disable this rule the http:// request is correctly handled. But when I enable it, I get this error.
Yet another update:
If I replace this:
<action type="Rewrite" url="https://abc.com/{R:1}" />
with
<action type="Redirect" url="https://abc.com/{R:1}" />
It all works out pretty well.
I was having the exact same issue. I installed Application Request Routing component and then set the Proxy enabled and selected the Use URL Rewrite to inspect incoming requests entered the redirect url alias and mine worked

Forcing HTTPS and avoid duplicate URLS using IIS7 URL Rewrite Module

I need to force every request to https://www.mysite.com (always with https and www)
The site is hosted in GoDaddy and I need to do it via IIS7 URL Rewrite Module.
I've been able to do the HTTPS redirect with the following code:
<system.webServer>
<rewrite>
<rules>
<rule name="Canonical Host Name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mysite\.com$" />
</conditions>
<action type="Redirect" url="https://www.mysite.com/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
Test cases
http://mysite.com -> https://www.mysite.com OK
http://www.mysite.com -> https://www.mysite.com NOT WORKING
I guess the condition is not being satisfied when I enter www.mysite.com in the browser, so there's no redirect and the page serves as HTTP instead of HTTPS.
I think I just need to modify the condition pattern, but I have almost nothing regex knowledge and I need this asap.
Thanks!
emzero, I think the issue is that your condition only matches precisely mysite.com:
<conditions>
<add input="{HTTP_HOST}" pattern="^mysite\.com$" />
</conditions>
Note the pattern: ^mysite\.com$. This says, in English, that the incoming URL must start with mysite.com and end with mysite.com, meaning www.mysite.com will not be matched.
Try this pattern instead, which allows for an option www.:
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)?mysite\.com$" />
</conditions>
Happy Programming!

Resources