I have searched around Google and StackOverflow trying to find a solution to this, but they all seem to relate to ASP.NET etc.
I usually run Linux on my servers but for this one client I am using Windows with IIS 7.5 (and Plesk 10). This being the reason why I am slightly unfamiliar with IIS and web.config files. In an .htaccess
file you can use rewrite conditions to detect whether the protocol is HTTPS and redirect accordingly. Is there a simple way to achieve this using a web.config file, or even using the ‘URL Rewrite‘ module that I have installed?
I have no experience with ASP.NET so if this is involved in the solution then please include clear steps of how to implement.
The reason for me doing this with the web.config and not PHP is that I would like to force HTTPS on all assets within the site.
10
Answers
You need URL Rewrite module, preferably v2 (I have no v1 installed, so cannot guarantee that it will work there, but it should).
Here is an example of such web.config — it will force HTTPS for ALL resources (using 301 Permanent Redirect):
P.S.
This particular solution has nothing to do with ASP.NET/PHP or any other technology as it’s done using URL rewriting module only — it is processed at one of the initial/lower levels — before request gets to the point where your code gets executed.
A simple way is to tell IIS to send your custom error file for HTTP requests. The file can then contain a meta redirect, a JavaScript redirect and instructions with link, etc… Importantly, you can still check “Require SSL” for the site (or folder) and this will work.
For those using ASP.NET MVC. You can use the RequireHttpsAttribute to force all responses to be HTTPS:
Other things you may also want to do to help secure your site:
Force Anti-Forgery tokens to use SSL/TLS:
Require Cookies to require HTTPS by default by changing the Web.config file:
Use the NWebSec.Owin NuGet package and add the following line of code to enable Strict Transport Security (HSTS) across the site. Don’t forget to add the Preload directive below and submit your site to the HSTS Preload site. More information here and here. Note that if you are not using OWIN, there is a Web.config method you can read up on on the NWebSec site.
Use the NWebSec.Owin NuGet package and add the following line of code to enable Public Key Pinning (HPKP) across the site. More information here and here.
Include the https scheme in any URL’s used. Content Security Policy (CSP) HTTP header and Subresource Integrity (SRI) do not play nice when you imit the scheme in some browsers. It is better to be explicit about HTTPS. e.g.
Use the ASP.NET MVC Boilerplate Visual Studio project template to generate a project with all of this and much more built in. You can also view the code on GitHub.
The excellent NWebsec library can upgrade your requests from HTTP to HTTPS using its
upgrade-insecure-requests
tag within theWeb.config
:To augment LazyOne’s answer, here is an annotated version of the answer.
Clear all the other rules that might already been defined on this server. Create a new rule, that we will name “Redirect all requests to https”. After processing this rule, do not process any more rules! Match all incoming URLs. Then check whether all of these other conditions are true: HTTPS is turned OFF. Well, that’s only one condition (but make sure it’s true). If it is, send a 301 Permanent redirect back to the client at
http://www.foobar.com/whatever?else=the#url-contains
. Don’t add the query string at the end of that, because it would duplicate the query string!This is what the properties, attributes, and some of the values mean.
MatchAll
) or any of the conditions must be true (MatchAny
); similar to AND vs OR.match
and itsconditions
are all true.redirect
(client-side) orrewrite
(server-side).https://
with two server variables.url
or not; in this case, we are setting it to false, because the{REQUEST_URI}
already includes it.The server variables are
{HTTPS}
which is eitherOFF
orON
.{HTTP_HOST}
iswww.mysite.com
, and{REQUEST_URI}
includes the rest of the URI, e.g./home?key=value
#fragment
(see comment from LazyOne).See also: https://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference
The accepted answer did not work for me.
I followed the steps on this blog.
A key point that was missing for me was that I needed to download and install the URL Rewrite Tool for IIS. I found it here. The result was the following.
I was not allowed to install URL Rewrite in my environment, so, I found another path.
Adding this to my web.config added the error rewrite and worked on IIS 7.5:
Then, following the advice here: https://www.sslshopper.com/iis7-redirect-http-to-https.html
I configured the IIS website to require SSL and created the html file that does the redirect (redirectToHttps.html) upon the 403 (Forbidden) error:
I hope someone finds this useful as I could not find all of the pieces in one place anywhere else.
In .Net Core, follow the instructions at https://learn.microsoft.com/en-us/aspnet/core/security/enforcing-ssl
In your startup.cs add the following:
To redirect Http to Https, add the following in the startup.cs
I am using below code and it perfect works for me, hope it will help you.
You have to configure redirection as below, when you are using IIS 10 with application load balancer on AWS.
If you have sub domain redirection use below: