IIS rewrite rule within subfolder - iis-7

I have 2 separate sites. One site is within a subfolder of the main site. I need to force HTTPS to both sites but keeping the query string intact as users can enter the site from internal pages.
The issue I am having is when I try to add the IIS rewrite rule for the subfolder site it redirects to the main site.
The main site rule seems to be overriding the subfolder rule.
Main site rule:
<rule name="Force HTTPS" enabled="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
Subfolder rule (in its own web.config file):
<rule name="Force HTTP" enabled="true" stopProcessing="true">
<match url="(^subfoldersite/.*)" ignoreCase="true" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
I have tried without a subfolder rule but same thing happens

You may want to check out this question's answer, in case it applies to your situation:
Override an IIS rewrite rule for child site?

Too late for this answer but I hope it may help someone. I just fixed this issue for my hosting.
You may use below settings.
Main Website Rule:
<rules>
<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" />
</rule>
</rules>
Subfolder Rule:
<rules>
<clear />
</rules>

Related

Http to Https URL rewriting

My requirement is straight forward. i have Http://abc need to make it Https://abc . I have added the folowing code in web.config. i.e. added new rule in IIS.
I have folowwed the URL rewriting module in IIS.
<rewrite>
<rules>
<rule name="http to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^off$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
</rule>
</rules>
</rewrite>
But still it doesn't work for me. Help me out.
The code you are using is very similar to what I use in production except that I use redirectType="Permanent" and I happen to use a hard coded domain name to ensure a canonical domain and I use {R:1} for the path and query but I think using {HTTP_HOST}{REQUEST_URI} as you are should work however you may need a slash between the two.
Give this a try:
<system.webServer>
<rewrite>
<rules>
<rule name="http to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^off$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>

Web.config URL Rewrites - HTTPS and Non-WWW

I need to have both https and non-www rewrites, while also NOT HARDCODING the domain, since we have numerous servers. This needs to be in the web.config, not in IIS.
I've read numerous articles:
http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference
http://madskristensen.net/post/url-rewrite-and-the-www-subdomain
how to set asp.net web.config rewrite http to https and www to non-www
The https rewrite works, the non-www does not.
<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="Permanent" />
</rule>
<rule name="Remove WWW" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<!--<add input="{CACHE_URL}" pattern="*://www.*" />-->
<!--<add input="{HTTP_HOST}" pattern="*://www.*" />-->
<add input="{HTTP_HOST}" pattern="^.*www.*" />
</conditions>
<action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="Permanent" />
// i've also tried
// url="{C:2}/{R:1}"
// url="{C:1}/{C:2}"
</rule>
I tested the regex for ^.*www.* on a regex tester and it was matching www.testing.com but not testing.com - so I would assume the pattern would catch it.
I need the URLs to redirect from:
testing.com ---> https://testing.com
www.testing.com ---> https://testing.com
www.testing.com/xyz/ ---> https://testing.com/xyz/
Was my own issue - there was no DNS for the www, therefore the redirect wouldn't resolve on it's own.
Code used:
<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="Permanent"/>
</rule>
<rule name="Remove WWW" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{CACHE_URL}" pattern="*://www.*" />
</conditions>
<action type="Redirect" url="{C:1}://{C:2}" redirectType="Permanent" />
</rule>
Add a "Canonical domain name" rule.
The soulution Rob found works fine as well. Mine seems to be a bit easier IMHO.

Site getting redirect to www.www.sitename.com

I am having this problem with my site getting redirect to www.www.sitename.com.
I have this code written in web.config which working fine for my other website.
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Redirect to WWW" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.([.a-zA-Z0-9]+)$" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
This is 100% wokring code other sites but having problem of www.www.sitename.com on this site srilanka-tours.co
How about doing it less generic and more focused on your actual host name?
E.g.:
<rule name="Redirect to WWW">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.srilanka-tours\.co$" negate="true" />
</conditions>
<action type="Redirect"
url="http://www.srilanka-tours.co/{R:1}"
redirectType="Permanent" />
</rule>
(This is an excerpt from a real-world rewrite rule).
The above rule is similar to what you get when using the "Canonical Domain Name" in the IIS Managers' "wizard" for creating rewrite rules. See the article on Scott Guthrie's blog.

http to https in IIS

I realize this is a simple question, but I'm just not finding the answer. I've applied the below rule...
<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>
The url I'm interested in has the pattern
http://[domain]/[directory]/[aspx page]
So http://example.com/funstuff/thefair.aspx
The result of the rewrite is http://[domain]/[an_aspx_page]
So the rewrite is removing the directory.
I assumed the {R:1} specifies a parameter that will be rewritten, and I tried https://{HTTP_HOST}/{R:1}/{R:2}, but this result in a 500 error.
I want to direct all traffic on this domain to https without changing the rest of the url entered by the user.
Here's what we use on one of our sites to auto-redirect all traffic to the https version. It also includes everything else in the url.
<rewrite>
<rules>
<clear />
<rule name="Redirect to https" enabled="true" 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="true" />
</rule>
</rules>
</rewrite>

Rewriting URLs from https:// to http:// in IIS7

I'm trying to rewrite urls from the form:
https://example.com/about
to the form
http://example.com/about
using IIS7 URL rewriting:
<!-- http:// to https:// rule -->
<rule name="ForceHttpsBilling" stopProcessing="true">
<match url="(.*)billing/(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="false" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
<!-- https:// to http:// rule -->
<rule name="ForceNonHttps" stopProcessing="true">
<match url="(.*)billing/(.*)" ignoreCase="true" negate="true" />
<conditions>
<add input="{SERVER_PORT}" pattern="^443$" />
</conditions>
<action type="Redirect" redirectType="Found" url="http://{HTTP_HOST}{REQUEST_URI}" />
</rule>
I'm at a loss; I've been browsing the web for examples and trying every syntax I can think of. The rewrite rules I specify simply don't appear to work at all for any https requests, as if all the https:// requests are flat out invisible to the rewrite engine.
rules work fine; see answer below.
Turns out that I had port :443 bound to a different website!
The above rewrite rules work fine for http:// to https:// rewriting and vice-versa -- though there might be more optimal or simple ways to do it.
Leaving this question here for future voyagers to find, as I didn't see many good examples of the https:// to http:// rewriting scenario on the web.
This post is a little old, but I wanted to answer. I am using ASP.Net MVC3, and Fabio's answer above didn't work for me. The easiest solution I came up with to handle the https redirect to http, while still allowing valid https pages to request secure content was just to add Whitelist rules above my https/http redirects:
<rule name="WhiteList - content folder" stopProcessing="true">
<match url="^content/"/>
<conditions logicalGrouping="MatchAll" trackAllCaptures="false"/>
<action type="None"/>
</rule>
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)billing/(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/billing/" redirectType="SeeOther" />
</rule>
<rule name="ForceNonHttps" stopProcessing="true">
<match url="(.*)billing/(.*)" ignoreCase="true" negate="true" />
<conditions>
<add input="{SERVER_PORT}" pattern="^443$" />
</conditions>
<action type="Redirect" redirectType="Found" url="http://{HTTP_HOST}{REQUEST_URI}" />
</rule>
please first consider binding https to your website for making below redirect module to work is essential (so bind your web app with a self-signed or valid certificate)
final part in web.config to redirect https to http:
<rewrite>
<rules>
<rule name="Force NonHTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="on" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/{REQUEST_URI}" />
</rule>
</rules>
</rewrite>
if you need the equivalent IIS GUI in rewrite module see below image
source: look tech-net for more detail and step by step guide.
Your solution work, but the problem is: your second instruction kill first instruction for any links is not (.)billing/(.), including your css, js, and images.
You can use this https to http rule:
<rule name="HTTPS to HTTP redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{RequiresSSL:{R:1}}" pattern="(.+)" negate="true" />
<add input="{HTTPS}" pattern="on" ignoreCase="true" />
<add input="{REQUEST_URI}" pattern="^(.+)\.(?!aspx)" negate="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="http://{HTTP_HOST}/{R:1}" />
</rule>

Resources