Need a rule for IIS Rewrite for https - iis

I have to add a rule for the following and can't get it to work.
The website is available under https www.domain.com. The certificate only works for that. If I try with https domain.com I get an 404 error. How can I redirect from https domain.com to https www.domain.com?
Any ideas?
I already covered when someone uses http und will be redirected to https.
Sorry, I couldn't write the complete url because I have not enough reputation. So I cut out the ://

You Can try This
<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="SeeOther" />
</rule>

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}" />

IIS 7.5 - Force canonical AND https using certificate issued with www

We purchased an SSL certificate for one of our domains. The certificate was issued to www.ourdomain.com. I am having great difficulty writing web.config rewrite rules to force both www and https. The problem is the certificate itself. Again, the certificate was issued to the www variant of our domain. As such it is only valid if www is present in the URL. Currently, I am using the following rule:
<rule name="Redirect to WWW" stopProcessing="true" >'
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\." negate="true"/>
</conditions>
<action type="Redirect" url="https://www.{HTTP_HOST}{HTTP_URL}" redirectType="Permanent" appendQueryString="false" />
</rule>
<rule name="Redirect to HTTPS">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="OFF"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{HTTP_URL}" redirectType="Permanent" appendQueryString="false" />
</rule>
http ourdomain.com is OK. http www.ourdomain.com is OK. https www.ourdomain.com is OK. https ourdomain.com FAILS with a browser error
I believe it fails because IIS processes SSL before it gets to these rules in the web.config. And because the certificate requires www to be requested, the user browser gets an error. On the error page, you can click "continue anyways" and the web.config rules kick in and force the redirection. Can this be fixed or do I need to buy a new certificate without www in the domain? Other certificates I have installed on the server without www in the domain work for all 4 URL examples.
Update: still looking for feedback

URL Rewriting help using IIS Rewrite

Please help me redirecting my URL using IIS rewrite the following:
https://domain.com:8080/Context/services/.*
to
https://domain.com:8443/Context/services/.*
We only have 8080 port open and that's why need redirection for SSL.
But at the same time I don't want any other URLs getting redirected such as
https://domain.com:8080/Context/xyz (or anything except services)/.*
I tried the following but it is not working:
<rule name="HTTPS Request on 8080 Redirect to HTTPS Request on 8443 for Root"
patternSyntax="ECMAScript" stopProcessing="true">
<match url="https(.*)Context/services/.*" />
<conditions>
<add input="{SERVER_PORT}" pattern="8080" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}:8443/{R:0}" />
</rule>
Give this a try:
<rule name="HTTPS Request on 8080 Redirect to HTTPS Request on 8443 for Root"
patternSyntax="ECMAScript" stopProcessing="true">
<match url="^(Context/services/.*)" ignoreCase="true" />
<conditions>
<add input="{SERVER_PORT}" pattern="8080" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}:8443/{R:0}" />
</rule>
If you review the Accessing URL Parts section here, you will notice that the match URL contains everything after the root of the directory it is placed in. For something in the root directory as is your case, it will be everything after the hostname and port without a leading slash /.

Resources