Forcing HTTPS and avoid duplicate URLS using IIS7 URL Rewrite Module - asp.net

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!

Related

HTTP to HTTPS Redirect in IIS

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.

URL Rewrite not working on IIS 10.0

I'm using URL Rewrite on IIS 10.0 and have the following rule configured at the server level (applicationHost.config). I've tried it in my web.config to no avail as well.
<rewrite>
<globalRules>
<rule name="redirect">
<match url="/admin" />
<conditions>
<add input="{REMOTE_ADDR}" pattern="10.30.*.*" negate="true" />
</conditions>
<action type="Rewrite" url="/error" />
</rule>
</globalRules>
</rewrite>
Is there anything immediately obviously wrong here? I want any external traffic trying to hit /admin to get redirected to an error page, and only allow a single internal IP block to access it. Pulling my hair out over here.
You may need to install Application Request Routing, which is an extension to IIS and is available here: https://www.iis.net/downloads/microsoft/application-request-routing
There is a problem in match regexp. It shouldn't start with slash. Correct is ^admin (^ means start of url)
<rule name="redirect">
<match url="^admin" />
<conditions>
<add input="{REMOTE_ADDR}" pattern="10.30.*.*" negate="true" />
</conditions>
<action type="Rewrite" url="/error" />
</rule>
And i have couple of notes:
1) For IP validation better to have regexp like that: 10.30.[0-9]{1,3}.[0-9]{1,3} instead of 10.30.*.*
2) Depends on your load balancer and network infrastructure, but you might need to check {HTTP_X_Forwarded_For} header instead {REMOVE_ADDR}, because client's IP might be in different header

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.

IIS 7.0 URL REWRITE

I am setting up a redirect to WWW for one of our sites in the web.config and ran into a small issue. The code I have in the web.config for the rewrite is as follows :
<rewrite>
<rules>
<rule name="Redirect to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="example.com" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" />
</rule>
</rules>
</rewrite>
I'm finding that it's actually working a little too well. Because of the pattern "example.com", I'm seeing that it's now redirecting to our live site on dev and staging because our URLS are laid out like so : dev.example.com & staging.example.com. For the time being, I have just commented out the rewrite on these other web.configs but I'm wondering if there's a better pattern or option to get around this issue.
If you only want the root domain without subdomains then you should edit your pattern in the HTTP_POST section.
Place a ^ in front of the pattern which means start with. So If the url starts with example.com then it gets redirected to www.example.com.
If its dev.example.com, this rule will be ignored.
Edit your example:
<add input="{HTTP_HOST}" pattern="^example.com" />

Conditional https redirect on IIS 7

I have the following rule on the site to redirect http to https. We just found out though that our app got submitted with just an http for the api. Until we can get this updated I need the site to ignore calls to the /api folder and only redirect everything else. I'm sure there's a way to say something like if URL does not contain /api/ then redirect.
<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="Found" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
Add an entry similar to <add input="{R:0}" pattern="/api(/|$)(.*)" negate="true" /> so that the whole file is:
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{R:0}" pattern="/api(/|$)(.*)" negate="true" />
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
Example URL: http://site.com/api/function
So, if the URL after the site matches any of the following it will stop processing (and thus not push the user to https)
/api
/api/anything
Any https URL
We run into the same kind of thing with a large application run in IIS behind a reverse proxy. The URL rewrite addon for IIS (that you appear to be using) is a bit of a pain, but it does the job really well and tolerates the MVC framework.
As you mentioned, simply putting a rewrite block in an API directory won't work because with MVC there are no directories. You would think MS would have a better solution for this -- but they don't. It makes things all the more challenging.
If you place a separate Web.config file in the /api application or directory you can override whatever rules apply for the site as a whole.
Check out Tip #1 in this article, and if you have the time read them all:
http://weblogs.asp.net/jgalloway/archive/2012/01/17/10-things-asp-net-developers-should-know-about-web-config-inheritance-and-overrides.aspx
John Galloway's blog is a fantastic resource for all things IIS and ASP.NET.

Resources