Rewrite URL on IIS from http to https does not work,

I have a problem. On IIS , I have a website with two ports 80 and 443(https) . I want to redirect all http requests from the user to https . I also added a Rewrite rule to https , but when I enter the browser http://localhost/site , it gives me the same page. I need to redirect the user to httpS://localhost/site .

Maybe this is due to my local configurations?

And disable Require SSL on IIS .

 The rule is: <rewrite> <rules> <rule name="HTTPS Redirect"> <match url="(.*)" ignoreCase="false" /> <conditions> <add input="{HTTPS}" pattern="off" ignoreCase="false" /> </conditions> <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}{REQUEST_URI}" /> </rule> </rules> </rewrite> 

Thanks.

+4
source share
4 answers

The following is the exact rule that we use on the IIS 7 website to redirect the entire request from HTTP to HTTPS.

 <configuration> <system.webServer> <rewrite> <rules> <rule name="Redirect to HTTPS" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTPS}" pattern="^OFF$" /> </conditions> <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found" /> </rule> </rules> </rewrite> </system.webServer> </configuration> 

There are some slight differences between what you publish and what we use. In addition, since you are working on a local host, you would not use the integrated web server with visual studio, would you? I do not think that it will handle IIS rewrite rules.

+14
source

I understand that this may not be your problem, but I had a similar debacle caused by another problem.

I turned on Require SSL, and this made the site constantly return 403. Therefore, to use this method, you must disable the SSL settings → Require SSL.

Hope this helps someone.

+3
source

You can add this to your pages. for redirection.

 if (!Request.IsSecureConnection) { Uri uri = new Uri(Request.Url, Request.RawUrl); Response.Redirect(string.Format("https://{0}{1}{2}", uri.Host.StartsWith("www.x.com") ? uri.Host : "www.x.com", uri.AbsolutePath, uri.Query)); } 

I just saw the mvc tag

add this attribute to your actions. or controllers.

 [RequireHttps] 
0
source

In addition, if you have several rules, the order may make a difference. Be sure to add the redirection rule before other rules or the redirection may not work.

Here is an example that uses a SPA that requires a rule order.

  <rules> // Adding this as last rule will cause it to not redirect <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="static dist files" stopProcessing="true"> <match url="^(.+)" /> <conditions> <add input="{APPL_PHYSICAL_PATH}app\{R:1}" matchType="IsFile" /> </conditions> <action type="Rewrite" url="/app/{R:1}" /> </rule> <rule name="index.html as document root" stopProcessing="true"> <match url="^$" /> <action type="Rewrite" url="/app/" /> </rule> <rule name="SPA Routes" stopProcessing="true"> <match url=".*|.*/.*$" /> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" /> </conditions> <action type="Rewrite" url="/app/" /> </rule> </rules> 
0
source

Source: https://habr.com/ru/post/1441515/


All Articles