URL Rewrite on IIS from http to https is not working, - asp.net-mvc

I have a problem. On IIS I got a web-site with two ports 80 and 443(https). I want to redirect all the http requests from user to https. I also added Rewrite rule to https, but when I enter in browser http://localhost/site it gives me the same page. I need to redirect user to httpS://localhost/site.
Maybe this is because of my local configurations?
And I 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>
Thank you.

Below is the exact rule we use on a production IIS 7 site to redirect all 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 minor differences between what you have posted and what we use. Also, since you are running on local host, you wouldn't be using the built-in web server with visual studio would you? I don't think it will process IIS rewrite rules.

I realize this may not be your issue, however I had a similar debacle that was cause by a different problem.
I had enabled Require SSL and that caused the site to continually return a 403. So to use this method it appears you must disable SSL Settings -> Require SSL.
Hope this helps someone.

Also, if you have more than one rule, order could matter. Be sure to add the redirect rule before other rules or redirect may not fire.
Here is an example that is using SPA that required the 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>

It works for me in IIS 8.5
redirect
Three points:
Use single word OFF rather than ^OFF$
Use url="https://{HTTP_HOST}/{REQUEST_URI}"
Use redirectType=Permanent rather than Found although both are working but preferable Permanent type in this case
The webconfig code
<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}/{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>

you can add this to your pages. to force 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 seen your tag of mvc
add this attribute to your actions. or controllers.
[RequireHttps]

Related

IIS web.config : Rewrite URL file as subdomain

I am trying to rewrite my url with a web.config file.
Actual URL: sudomain.mydomain.com/legal.html
Wished URL: legal.mydomain.com
I have already hide the html extension.
<rule name="Hidehtml" enabled="true">
<match ignoreCase="true" url="Legal" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{URL}" pattern="(.*)\.(.*)" negate="true" />
</conditions>
<action type="Rewrite" url="Legal.html"/>
</rule>
I saw this kind of rewrite is possible with subfolder, but I wonder if it is possible with a file.
I am trying this one but it give me a DNS error.
<rule name="testrule" stopProcessing="true">
<match url="legal(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="mydomain.com" />
</conditions>
<action type="Redirect" url="http://legal.mydomain.com" />
</rule>
I wonder if I am not missing something, maybe a redirect is needed too?
Thanks for your help !
To achieve this,
I just registered two CNAME for my website.
FQDN:subdomain.mydomain.com
FQDN:legal.mydomain.com
Then I apply them to my IIS site.
Just make sure both of them can be accessed in web browser.
Then I apply following rewrite rules
<rules>
<rule name="testrule" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{URL}" pattern="^/legal\.[a-zA-Z]+" />
<add input="{HTTP_HOST}" pattern="subdomain.candy.com" />
</conditions>
<action type="Redirect" url="http://legal.candy.com" />
</rule>
<rule name="rewrite rule">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="legal.candy.com" />
<add input="{URL}" pattern="^(/)?$" />
</conditions>
<action type="Rewrite" url="http://subdomain.candy.com/legal.html" />
</rule>
</rules>
When you access sudomain.mydomain.com/legal.html, the request will be redirected to legal.mydomain.com/ and legal.mydomain.com will rewrite back to /legal.html for response content.
Please keep in mind that legal.mydomain.com is always required to be registered in DNS even you just use it as a mask.
If your website are forwarding the public internet, please remember to purchase domain for legal.domain.com as well.

URL Rewrite not keeping full path

I'm trying to use the URL Rewrite module for IIS 8 to make sure all connections are https. What I cannot figure out is how to maintain the full path for the rule. I am new to using this so hopefully this is a simple fix.
I will go to a page like this : https://xyz.domain.com/mypath/default.aspx.
After removing the 's' from 'https', I get redirected to https://xyz.domain.com/default.aspx and the mypath path is gone. I need to maintain this.
Here is the webconfig entry:
<rewrite>
<rules>
<rule name="Force http to https" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
I encountered the same issue after following an example online. I changed the redirect url to be https://{HTTP_HOST}/{REQUEST_URI}, and it preserved the full URL in the redirected request.
For reference, see the online docs URL Rewrite Module Configuration Reference.
Try this instead:
<!-- Require HTTPS -->
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>

IIS URL Rewrite - IIS 7

I am new to using IIS Url Rewrite. I installed it and set up a rule using IIS, but nothing happens when I try to access a url in my localhost. Below is what IIS put in my web.config. When I access this url mysite/srcc_development_2012/login/default.aspx, based on the rule I set up it should redirect to google, but it does not work. What am I missing?
<rewrite>
<rules>
<rule name="SRCC" patternSyntax="Wildcard" stopProcessing="true">
<match url="mysite/srcc_development_2012/*" />
<action type="Redirect" url="http://www.google.com" />
<conditions logicalGrouping="MatchAny">
</conditions>
</rule>
</rules>
</rewrite>
i know this is an older thread but it is unanswered and shows up in the search..
here is how i would do this:
<rewrite>
<rules>
<rule name="SRCC" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{URL}" pattern="^srcc_development_2012(.*)?" />
</conditions>
<action type="Redirect" url="http://www.google.com" />
</rule>
</rules>
</rewrite>
The value in is what comes after the domain (stackoverflow.com/THIS/IS/THE-URL, where stackoverflow.com is the {HTTP_HOST} (but we arent using that for this rule)).
The below one should work :
But make sure you add this rule to the login folder.
<rule name="redirect" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="https://google.com" />
</rule>

rewrite rule not redirecting http to https

Currently we are trying to setup a redirect from htttp to https in our web.config limited to the domains we use. But it's not working.
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="^(?:http(?:s)?:\/\/)?(?:[^\\.]+\.)?mysite\.com(\/.*)?$" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
This rule works fine in a regex editor. But not on the webserver. http://rubular.com/r/3NQTozcigZ
^(?:http(?:s)?:\/\/)?(?:[^\\.]+\.)?mysite\.com(\/.*)?$
Looking at the failedreqlogfiles pattern_match is false. Any insights?
After doing some more testing I'm finding that this works.
http://sub.site.com/
But this will not work.
http://sub.site.com/Account/Logon
The pattern used in url is here only to be matched again the requested path (and only the path, not the url).
If you want to check the domain as well as if https is used or not, you have to rely on the conditions:
<rule name="Redirect mysite.com to HTTPS" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_HOST}" pattern="mysite\.com$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:0}" />
</rule>
This rule will check that both conditions are true (that is what logicalGrouping="MatchAll" is here for) on any path (.*):
The request was not using HTTPS
The domain requested ends with mysite.com
For reference: http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#Accessing_URL_Parts_from_a_Rewrite_Rule
Do you need the regex? If you're just redirecting all traffic to HTTPS, something like this should work:
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action
type="Redirect"
redirectType="Permanent"
url="https://{HTTP_HOST}{REQUEST_URI}"
appendQueryString="false"
/>
</rule>
I'd be inclined to use redirectType="Permanent" instead of "Found", since an HTTP 301 response is generally better for SEO.
If you're permanently moving your site to HTTPS, you might also want to look at sending the Strict-Transport-Security header. This will ensure that modern browsers never request your site over a non-secure connection.

IIS manager rewrite all URLS after mysite.com/

I'm trying to rewrite all the urls for my website.
For example, if the user types in mysite.com/index.aspx
it'll change the url in the top to www.mysite.com/index.aspx
Any suggestions?
Try this one
<rewrite>
<rules>
<rule name="Enforce canonical hostname" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.mysite\.com$" />
</conditions>
<action type="Redirect" url="http://www.mysite.com/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>

Resources