How do I exclude a directory from a web.config rewrite rule? - web-config

We have a rewrite rule to http to https, and that works fine. However, we have one directory "complaints" that we do not want the rule applied to. I thought I knew how to do this, and have researched it for the past couple hours and it seems correct, but it doesn't work.
This is the rule...
<rule name="https redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{REQUEST_URI}" pattern="^complaints$" negate="true"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
I have also tried it this way, which also does not work for me...
<rules>
<rule name="skip rule" stopProcessing="true">
<match url="^complaints$" />
<action type="None" />
</rule>
<rule name="https redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
I have also run IISRESET after making these changes.
Anyone know why this is not ignoring the "complaints" directory?

I removed the extra code from the web.config file that was working, and instead just created a web.config in the "complaints" directory with the following...
<rules>
<clear />
</rules>
This causes the "complaints" directory to ignore the rule inherited from the parent directory.

Related

How to rediret all requests to another host in IIS

I want to redirect all requests from the old domain to a new domain. For example:
From: oldhost.com/app1/houses?city=springs
To: newhost.com/app1/houses?city=springs
I've tried using both the basic "HTTP Redirect" as far as "URL Rewrite" module with the same results. Redirect only works to a limited extent, as long as there is nothing past the first path segment:
oldhost.com/app1 - works
oldhost.com/app1/houses?city=springs - does not work
Here is the attempted URL Rewrite rule:
<rule name="rule1" enabled="true" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="https://newhost.com/{C:1}" logRewrittenUrl="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="oldhost.com(.*)" />
</conditions>
</rule>
You can try this rule:
<rule name="test">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^oldhost.com$" />
</conditions>
<action type="Redirect" url="https://newhost.com/app1{R:1}" />
</rule>
Got it to work with the following rule:
<rule name="my_redirect" enabled="true" stopProcessing="true">
<match url=".*" />
<action type="Redirect" url="https://newhost.com/{R:0}" logRewrittenUrl="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="oldhost.com" />
</conditions>
</rule>

IIS Redirect Rule to redirect one url to another

I Know this has been asked a lot but I just can't seem to figure out how to get this to work, even looking at other variables.
I need to redirect www.example.com/article-2 to www.example.com/article
<rule name="Redirect Dup Artivcle URL" enabled="false" patternSyntax="Wildcard" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}{REQUEST_URI}" pattern=" https://www.example.com/article-2/" />
</conditions>
<action type="Redirect" url="https://www.example.com/article/" appendQueryString="false" />
</rule>
I feel like I may have the wrong URL matching pattern...
Its obviously that {HTTP_HOST} is not covering https://.
Please modify the rule like this
<rule name="Redirect Dup Artivcle URL" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="www.example.com/article-2/" />
<add input="{HTTPS}" pattern="^on$" />
</conditions>
<action type="Redirect" url="https://www.example.com/article/" appendQueryString="false" redirectType="Temporary" />
</rule>

iis url redirect http to non www https

i need to redirect from
www.domain.de to https://domain.de
-works
http://www.domain.de to https://domain.de
-works
http://domain.de to https://domain.de
-does not work
rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^www\.(.+)$" />
</conditions>
<action type="Redirect" url="https://{C:1}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
I think this will work for you, the search pattern has the optional www and redirects using the back reference C:2, the rule has a condition to only run against non https.
This is the pattern:
"^(www\.)?(.*)$"
where:
{C:0} - www.domain.de
{C:1} - www.
{C:2} - domain.de
Here's the rule in full:
<rewrite>
<rules>
<rule name="SecureRedirect" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTPS}" pattern="off" />
<add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:2}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
The accepted answer doesn't handle the special case https://www.domain.de.
These rules do the complete job:
<rewrite>
<rules>
<rule name="Redirect to HTTPS without www" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^OFF$" />
<add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Special case for HTTPS with www" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTPS}" pattern="^ON$" />
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
If you want to redirect www to non www:
Add DNS entry for www.yourdomain.com to refer to your server's public IP
Then you need to Edit Bindings of your website and "Add Binding" www.yourdomain.com
Add a rewrite rule to your website using iis:
<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>
Reference: http://madskristensen.net/post/url-rewrite-and-the-www-subdomain
If you want something more flexible than for your three examples, change your HTTP_HOST pattern to : \w+\.\w+$. That would work for all three examples plus anything else, like subdomain.abcdef.domain.de.
If you use this regex either encase it in parenthesis or change C:1 to C:0 in your action.
This is what worked for me:
<rule name="NameRule1" stopProcessing="true" enabled="true" >
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://example.com/{R:1}" />
</rule>
I got it from: https://medium.com/iis-and-windows-server/redirect-url-from-www-to-non-www-in-iis7-5-4d2909b9704
Redirecting https://www.example.com to https://example.com I had to do 2 steps
Select the website, click "URL Rewrite" -> Click "Add Rule" -> Choose "Cononcial domain name" -> select your domain (without the www) -> make sure rule is at the top -> highlight the rule and click "Edit" -> and update "Redirect URL" at the end of the rule to include the 's' - https://example.com/{R:1}
I had to also create a new website with the domain www.example.com, new app pool and select the SSL cert and point it to a folder containing the following (for some reason StackOverflow wont display the code I pasted below for the rewrite rule but just google IIS rewrite rule)
After doing both steps only then does it work as desired
Tried many of solutions but below works for me:
Right click on Domain and Add a Site bindings, add one more domain with www:
2. Restart services. it should work.
These rewrite rules matches the following URL's:
www.example.com
http://www.example.com
https://www.example.com
They will all redirect to: https://example.com
These rewrite rules may redirect twice because of the separate rules. (I'm a newbie with regex)
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="HTTPS" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
<rule name="WWW" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="https://example.com{PATH_INFO}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

IIS 7 rewrite rule - redirect based on presence of querystring

Is it possible to redirect using web.config based on the presence of a querystring in the initially requested URL? I'm not sure what to include in the the conditions. I'm a beginner at working with rewrite/redirect rules in web.config and would like to learn more about syntax, parameters etc.
I'm trying to do something like this:
<rewrite>
<rules>
<rule name="Restricted Folder with Querystring" stopProcessing="true">
<match url="^test folder/(.*)" ignoreCase="false" />
<conditions>
<!--redirect to a certain page if a certain querystring is present -->
</conditions>
<action type="Redirect" redirectType="Permanent" url="/test folder/{R:1}" />
</rule>
<rule name="Restricted Folder without Querystring" stopProcessing="true">
<match url="^test folder/(.*)" ignoreCase="false" />
<conditions>
<!--redirect to another page if querystring is not present -->
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://www.whatever.com/page.asp?url={R:1}" />
</rule>
</rules>
</rewrite>
deeholzman, I believe for your Condition - Add Input example you want to use "pattern" and not "matchType". EX:
<add input="{QUERY_STRING}" pattern="^whateverpattern" />
or
<add input="{QUERY_STRING}" pattern="^whateverpattern" negate="true" />
I found the answer in the URL Rewrite Module Configuration Reference. In the rewrite section of web.config in whatever folder you wanted to perform the redirect from, it would go something like this:
<rules>
<rule name="Restricted Folder Gimbal" stopProcessing="false">
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{QUERY_STRING}" pattern="^whateverpattern" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="http://www.whatever.com/file.html" />
</rule>
</rules>
For a querystring not present, you could add the 'negate' parameter to the condition:
<add input="{QUERY_STRING}" pattern="^whateverpattern" negate="true" />
The the URL Rewrite Module Configuration Reference is a very handy reference that unfortunately took me longer than expected to find.

web.config redirect multiple domains to one

Hi i am trying to redirect my domain aliases to one domain.
I currently have this rule
<rule name="WWW Rewrite" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true"
pattern="^www\.([.a-zA-Z0-9]+)$" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:0}"
appendQueryString="true" redirectType="Permanent" />
</rule>
It works perfect when the alias doesnt have the www in front.. how do i say redirect all that is not equal to this domain
thanks
Try it. I'm not sure if it works or not, I'm not great in this subject but this has been sitting here for 4 months unanswered, so I thought I'd give it a wollop.
<rule name="Rewrite domain requests" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?([.a-zA-Z0-9]+)$" />
</conditions>
<action type="Rewrite" url="http://www.mydomain.com/url={R:1}" appendQueryString="true" />
</rule>
It's the pattern I'm unsure of. I think this says, match anything in the URL, whether with or without www, and any possible domain extension.
Add one rule for each domain. It keeps query string too:
Lloyd Zhang : http://forums.iis.net/t/1185885.aspx
<rule name="Domain Redirect" stopProcessing="true">
<match url="(.*)" />
<action type="Redirect" url="http://{C:1}mydomainalias.com/{R:1}" redirectType="Permanent" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)?mydomain\.com" />
</conditions>
</rule>
This will solve your problem.
You have to negate the main domain to avoid a redirect loop.
<rule name="Rewrite domain requests" stopProcessing="true" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?([.a-zA-Z0-9]+)$" />
<add input="{HTTP_HOST}" pattern="^www\.domain\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:1}" redirectType="Permanent" appendQueryString="true" />
</rule>