Exclude Development URL from IIS rewriting rule - iis

I have a IIS URL - re-writing rule that makes sure all the urls have www. -
<rule name="Add www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.example.com" negate="true" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}" />
</rule>
This works in production the way I want it to.
However I have a local version of my website set up on my development machine that looks like http://mydomain.local
When I run my local version of the website and go to mydomain.local I get redirected to www.example.com
How can I make the rule ignore my local development URL?

You can try to add a condition to ignore the local development URL.
<rule name="Add www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.example.com" negate="true" />
<add input="{HTTP_HOST}" pattern="http://mydomain.local" negate="true" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:1}" />
</rule>
Best regards,
Sam

Related

Redirect from http to https on IIS 10.0

I have an aspx web app accessed via http://domain/web.aspx. This web app uses port 80 and http://domain:80/web.aspx works OK. I would like to redirect all calls to https://domain:82/web.aspx. I've tried using the rewrite rule
<rule name="HTTPS force" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(.*):80$" />
</conditions>
<action type="Redirect" url="https://localhost:82" appendQueryString="false" />
</rule>
in web.config in the same folder as web.aspx but this has no effect. What rewrite rule do I need to use?
You should use the following as input:
<rule name="HTTPS force" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="Permanent" />
</rule>
Not sure if you want the :80 there in the URL? If so, it can be added to my example too. Taken from here: Web.config redirects with rewrite rules - https, www, and more.
Found this rule worked
<rule name="HTTPS force" enabled = "true" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="http://domain/Web.aspx" />
</conditions>
<action type="Redirect" url="https://domain:82/Web.aspx"/>
</rule>

Create one rule to maintain multiple URLs

Windows Server 2012
I have 1 IIS container with 4 different URLs for one domain and 2 domains in total as below (set with the bindings)
www.site1.com
site1.com
www.site1.co.uk
site1.co.uk
www.site2.com
site2.com
www.site2.co.uk
site2.co.uk
www.site1.com - is the primary domain, meaning any combination of the URLs above must take the user to www.site1.com or www.site2.com
Below is the config i have for site 1 but site 2 is essentially the same. www.Site2.com not listed for brevity
<rule name="site1CoUk" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="site1.co.uk" />
</conditions>
<action type="Redirect" url="http://www.site1.com/{R:0}" />
</rule>
<rule name="site1WwwCoUk" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.site1.co.uk" />
</conditions>
<action type="Redirect" url="http://www.site1.com/{R:0}" />
</rule>
<rule name="site1Com" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="site1.com" />
</conditions>
<action type="Redirect" url="http://www.site1.com/{R:0}" />
</rule>
Everything seems to work but is there a way to reduce the configuration so i can maintain it within one rule (or 2 rules if i add in the www.site2.com). So i did something like this
<rule name="site1CoUk" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="site1.co.uk" />
<add input="{HTTP_HOST}" pattern="www.site1.co.uk" />
<add input="{HTTP_HOST}" pattern="site1.com" />
</conditions>
<action type="Redirect" url="http://www.site1.com/{R:0}" />
</rule>
but this doesnt work as soon as i add the second HTTP_HOST pattern.
The main reason is as i add more URLS it would easier to maintain under a one rule rather than adding a new rule each time round? Eventually the site would become https so i assume i could change the URL.

IIS http to https rewrite rule and exclude IP address

I wrote a http to https rewrite rule in IIS.
I want to exclude a http://XXX.XX.XXX.XX (one single IP address) from this.
Then I want to write another rule that rewrites those http://XXX.XX.XXX.XX and https://XXX.XX.XXX.XX to https://www.foo.com (my domain which I have ssl certificate for).
Here both rules:
<rule name="http to https redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{REQUEST_URI}" pattern="^/XXX.XX.XXX.XX$" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
<rule name="Redirect to my site">
<match url="XXX.XXX.XXX.XX" />
<conditions>
<add input="{QUERY_STRING}" pattern="XXX.XXX.XXX.XX" />
</conditions>
<action type="Rewrite" url="https:www.foo.com" />
</rule>
It does not work with the excluded pattern and I wonder if the second rule applies at all. Any advise is wellcome.

IIS URL Rewrite not redirecting subfolders

URL Rewrite rule for redirecting from http to https:
<rewrite>
<rules>
<rule name="http to https" enabled="false" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
However, when you go directly to a subfolder like http://www.example.com/employees via an old bookmark or by typing the address into the browser, it doesn't redirect to https://www.example.com/employees but stays on http://www.example.com/employees.
Oddly enough, we have the same rewrite rules for a dev site, and it will redirect from http://dev.example.com/employees to https://dev.example.com/employees.
Very strange. Any ideas?
This is what I use - I add it to the applicationhost.config file, so it's at the global level. It's a little different then yours, I run many different sites and never have the issue you're describing doing it this way.
<rule name="Root Redirect to HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{SERVER_PORT}" pattern="^80$" />
<add input="{HTTP_HOST}" pattern="example\.com$" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" />
</rule>

How to apply URL Rewrite rules correctly for IIS applications

We have an IIS 8.5 setup where a single website is bound to domain.com and contains a number of IIS applications accessed as domain.com/app1, domain.com/app2 etc.
Each of these applications points to the same physical path, so they all share a web.config. This is a specific CMS configuration.
I have applied the usual URL Rewrite rules (redirect to HTTPS, enforce lowercase, add trailing slash etc.) to the web.config each application shares but have realised these rules are only applied to the URL after the application name. The rules I have are just standard rules added using the URL Rewrite GUI:
<rewrite>
<rules>
<rule name="Enforce lowercase" stopProcessing="true">
<match url="[A-Z]" ignoreCase="false" />
<action type="Redirect" url="{ToLower:{URL}}" redirectType="Permanent" />
</rule>
<rule name="Add trailing slash" stopProcessing="true">
<match url="(.*[^/])$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}/" redirectType="Permanent" />
</rule>
<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>
So, for example, http://domain.com/APP1/PATH redirects to https://domain.com/APP1/path/. Also, https://domain.com/app1 doesn't redirect to https://domain.com/app1/.
The HTTPS rule is fine, but can anyone tell me how I can configure the other 2 rules so that they work with the entire URL, bearing in mind that the specific application name (app1, app2 etc) needs to be handled generically.
UPDATE
I've discovered that I can enforce lowercase URLs using a global rule in IIS (at server level) which is sufficient for my needs. But it doesn't seem to be possible to replicate the website-level rule for adding/removing trailing slash.
You just need to change action URL.
<rule name="Add trailing slash" stopProcessing="true">
<match url="(.*[^/])$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}/" redirectType="Permanent" />
</rule>

Resources