IIS rewrite rule for http to https with canonical host - iis

Environment:
Windows Server 2012 R2
IIS 8.5
Challenge
Completely new to rewrite rules and I need to implement a rule that does two things:
Change protocol from http to https
Change host name to www.example.com
Problem is I cannot really test this per se, because our SSL certificate only exists on our production site, so I need to make sure I get this right.
In doing the research I found the documentation regarding rewrite rules somewhat sparse, but cobbled together the following from various examples, etc.:
<rule name=”http_to_https_redirect">
<match url="(.*)" />
<conditions><add input="{HTTPS}" pattern="^OFF$" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}" redirectType="Permanent" />
</rule>
I still have no clue as to what {R:1} means or how it behaves as I found only a short blurb about it being a “Back-references to rule patterns are identified by {R:N} where N is from 0 to 9. Note that for both types of back-references, {R:0} and {C:0}, will contain the matched string.” (from: https://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference)
Question:
Is the above rule correct as per points 1 and 2 above?
Hopefully someone with a little more experience can confirm – yes / no ?
Also found this post:
IIS Url Rewrite rule HTTP to HTTPS AND add WWW
... but the actual rewrite rule was never posted in the answer!

The rewrite url to accomplish the redirect using https and the canonical host is as follows:
<rewrite>
<rules>
<rule name="redirect_http_to_https_with_canonical_host" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTPS}" pattern="off" />
<add input="{HTTP_HOST}" pattern="www\.example\.com" negate="true" />
</conditions>
<action type="Redirect" url="https://www.example.com{URL}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
The above works for the following cases:
Input: http://example.com
Result: https://www.example.com
Input: http://www.example.com
Result: https://www.example.com
Input: http://example.com/somepage.aspx
Result: https://www.example.com/somepage.aspx
Input: http://example.com/somepage.aspx?q=test
Result: https://www.example.com/somepage.aspx?q=test
Input: https://example.com
Result: https://www.example.com
Input: https://www.example.com
Result: https://www.example.com
Input: https://example.com/somepage.aspx
Result: https://www.example.com/somepage.aspx
Input: https://example.com/somepage.aspx?q=test
Result: https://www.example.com/somepage.aspx?q=test

Related

URL Rewrite giving 404

I have subdomain with an A record to my IIS Server:
sub.domain.com
The IIS server has a binding for sub.domain.com and a cert for *.domain.com
What we need is when someone goes to sub.domain.com it takes them to another site but masks the URL and keeps the SSL. The destination page has in its SAN cert sub.domain.com.
Tried a forward on Godaddy which works but it doesn't keep the SSL and we get cert issues. So thought was to point to our server to pass SSL and then redirect it. I tried a URL Rewrite but it's giving a 404.
URL Rewrite:
<rewrite>
<rules>
<clear />
<rule name="Pay Redirect" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^(www\.)?sub\.domain\.com$" />
</conditions>
<action type="Redirect" url="https://www.domain2.com/cgsdesktop/PaymentLanding/UniversalPortal" appendQueryString="false" />
</rule>
</rules>
</rewrite>
I just need to know how to get this done.
How about this:
<rules>
<rule name="Pay Redirect" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^sub.domain.com$" ignoreCase="true"/>
<add input="{HTTP_HOST}" pattern="^www.sub.domain.com$" ignoreCase="true"/>
</conditions>
<action type="Redirect" url="https://www.domain2.com/cgsdesktop/PaymentLanding/UniversalPortal" appendQueryString="false" />
</rule>
</rules>
use sub.domain.com instead of sub\.domain\.com.
This article contains some useful recipes on IIS redirect/rewrite rules.
In order to mask the URL and keep the SSL in the browser bar, we need to create URL Rewrite action rules of URL Rewrite extension.
However, URL Rewrite action only supports to redirect the request to the same domain by default. We have to install the Application Request Routing extension when redirecting the request to another website.
https://www.iis.net/downloads/microsoft/application-request-routing
or we will get an Http 404 error.
See my preceding post for more details.
ASP.net URL Rewrite subdirectory to external URL
Feel free to let me know if there is anything I can help with.

Exclude specific urls from http to https permanent redirect rule

I've got the following redirect rule below to force all http traffic to https. However, we need to serve some pages that have a problem displaying in https and we need to force them to be http until we can get them updated to work under https.
<rule name="Redirect to https" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
One thing all these pages have in commons in that "/SSLA/" is part of the url. I'm not the rewrite guru I would like to be, but there are a couple of potential ways I can think of to fix this:
a) Somehow update this rule to exclude urls with "/SSLA/".
B) Add a rule before this rule that catches anything with "/SSLA/" and redirects to http if it's going to https and (either in the same rule or a separate rule?) also does nothing for http requests but stops processing so it doesn't hit this global http to https redirect rule.
Problem is, I'm not great with either regex or rewrite rules so I'm not quite sure how to accomplish either of those solutions (actually not even sure even either are the "proper" way to handle this).
So... please help! :)
EDIT: I should note that I have updated the c# code to redirect to http for these pages, but of course that causes a loop with the above rule. But I just point this out to say that all I need is for the redirect rules not to force http requests for pages with "/SSLA" to https since the code will redirect any such https requests to http. But if it's a trivial matter to also have the redirect rule force https requests with "/SSLA/" in them to http then I could remove the c# code that does the same.
try use
<match url="((?!SSLA).)*" />
insteed
<match url=".*" />
You should use two rewrite rules:
For redirecting HTTPS -> HTTP for SSLA/* pages
For redirecting HTTP -> HTTPS for non SSLA/* pages.
Check my example below how I am filtering SSLA and non SSLA pages
<rule name="Redirect to http SSLA" stopProcessing="true">
<match url="^SSLA" />
<conditions>
<add input="{HTTPS}" pattern="on" ignoreCase="true" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
<rule name="Redirect to https" stopProcessing="true">
<match url="^SSLA" nagate="true" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>

IIS Web.config rewrite rule issue

I recently moved my website from http to https
My SSL certificate is for www.domain.com and not for domain.com, i.e. with www as prefix.
Now I facing an issue, when someone is accessing my website as http://www.domain.com it redirect to htts://www.domain.com without any issue as I have mentioned permanent redirect in web.config rewrite rules using following rules
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{REQUEST_URI}" negate="true" pattern="^/mobile.asmx*" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
However, when some is trying http://domain.com I am getting certificate error as SSL certificate is for domain with www prefix.
What I need help with is that rewrite should work as follows:
if request is from http://www.domain.com then move it to https://www.domain.com
however, if request is from http://domain.com then also move it to https://www.domain.com.
Please help if possible as the issue is with live website.
I got it, all I need is to add www.
<action type="Redirect" redirectType="Permanent" url="https://www.{HTTP_HOST}/{R:1}" />

URL Alias in IIS 8.5 Using URL Rewrite Module

I need to create easy to remember URL's that redirect to long and hard to remember paths for a large number of applications.
i.e.
subdomain.domain.edu/shortname
redirects to
https://www.subdomain.domain.edu/mainApplication/subfolder/page
I'm using the URL Rewrite module in IIS 8.5, but I keep getting a 404 when I browse to the short alias. I know the rewrite module is working as I use it to handle rewriting HTTP to HTTPS and to add WWW to a URL.
My rewrite rule looks like:
<rewrite>
<rules>
<rule name="Easy to remember shortcut" stopProcessing="true">
<match url=".*subdomain.domain.edu/shortname" />
<conditions>
<add input="{URL}" pattern=".*/shortname" />
</conditions>
<action type="Redirect" url="https://www.subdomain.domain.edu/mainApplication/subfolder/page.aspx" />
</rule>
</rules>
</rewrite>
Of course this returns a 404. Any ideas?
Forgive me if there is already an answer to this in another post, however, I've read through and tried over 30 posts on the URL rewrite module and have not yet found the solution for actually creating an alias.
Url rewrite match condition won't be able to see your domain i.e. if the URL is https://www.subdomain.domain.edu/shortname the match part can only see shortname (anything after domainname/).
To validate the host we need to add in the conditions clause. So your rule will be something like below
<rule name="shortnameURL" enabled="true" stopProcessing="true">
<match url="shortname" />
<action type="Redirect" url="mainApplication/subfolder/page" />
<conditions>
<add input="{HTTP_HOST}" pattern=".*subdomain.domain.edu" />
</conditions>
</rule>
Also it should be ok if you add the entire URL here
<action type="Redirect" url="mainApplication/subfolder/page" /> as below
<action type="Redirect" url="https://www.subdomain.domain.edu/mainApplication/subfolder/page" />

IIS 7 URL Rewrite difficulties

To configure an IIS rewrite rule to redirect traffic to my site to a different URL.
Basically I am trying to redirect this url: www.sharepointalex.co.uk to www.sharepointalex.co.uk/blog. At the moment it is pointing at the root of my site (a wedding website I setup for my partner and I).
www.sharepointalex.co.uk is a domain pointer on WinHost.
The rule I have so far is:
<rules>
<rule name="Blog" patternSyntax="ExactMatch" stopProcessing="true">
<match url="www.sharepointalex.co.uk" />
<action type="Redirect" url="{R:0}/blog" redirectType="Temporary" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.sharepointalex.co.uk" />
</conditions>
</rule>
</rules>
However this doesnt seem to work.
Any help would be greatly appreciated.
Cheers
The match URL should not include the host name.

Resources