IIS rewrite rule for processing all URLs except given - iis

Here is the IIS rewriting rule for some URLs to process:
<rule name="rule" stopProcessing="true">
<match url="^(word|world|hero|about)[/]?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/info.aspx?id={R:1}" />
</rule>
How can I create an opposite rule? For example, I want to process all URLs except "/special", "/escape". This:
<rule name="rule" stopProcessing="true">
<match url="^(?!(special)&?!(escape))[/]?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/info.aspx?id={R:1}" />
</rule>
does not work. "/special" and "/escape" URLs are processed as they should, but other URLs give me 404 pages.

You have to use the negate attribute in this case.
Your rule will become:
<rule name="rule" stopProcessing="true">
<match url="^(special|escape)/?$" negate="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/info.aspx?id={R:1}" />
</rule>

Related

Rewrite rule for a folder/page name without an extension

I have a rewrite rule that doesn't seem to be working as I anticipated. I'm redirected to a 404. It seems to only work if I have an extension on the page.
/folder/page.html works but /folder/page doesn't.
<rule name="preact" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(docs)" negate="true" />
</conditions>
<action type="Rewrite" url="index.html" />
</rule>

IIS Rewrite problems - catching all .php files

In a folder I have several .php files (more than 10)
aaa.php
bbb.php
ccc.php
ddd.php
index.php
What I'm trying to achieve is properly re-writing them so as to have friendly URLs. The rewrites are:
www.domain.com/data -> www.domain.com/data/index.php
www.domain.com/data/ -> www.domain.com/data/index.php
www.domain.com/data/aaa -> www.domain.com/data/aaa.php
www.domain.com/data/aaa/ -> www.domain.com/data/aaa.php
www.domain.com/data/aaa/chapter1 -> www.domain.com/data/aaa.php?c=chapter1
www.domain.com/data/bbb -> www.domain.com/data/bbb.php
www.domain.com/data/bbb/ -> www.domain.com/data/bbb.php
www.domain.com/data/bbb/chapter1 -> www.domain.com/data/bbb.php?c=chapter1
...
By using the following rules I achieve what I want for single php files
<rule name="Friendly1" stopProcessing="true">
<match url="^aaa$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="aaa.php" appendQueryString="false" />
</rule>
<rule name="Friendly2" stopProcessing="true">
<match url="^aaa/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="aaa.php" appendQueryString="false" />
</rule>
<rule name="Friendly3" stopProcessing="true">
<match url="^aaa/(.+)\.(.+)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.{R:2}" appendQueryString="false" />
</rule>
<rule name="Friendly4" stopProcessing="true">
<match url="^aaa/(.+)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="aaa.php?c={R:1}" appendQueryString="false" />
</rule>
That works. However it's not efficient and it's error prone copypasting those rules for every php file in the folder.
So, I thought of exchanging the aaa with (.+) in the rules and it doesn't work for some cases.
<rule name="Friendly1" stopProcessing="true">
<match url="^(.+)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.php" appendQueryString="false" />
</rule>
<rule name="Friendly2" stopProcessing="true">
<match url="^(.+)/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.php" appendQueryString="false" />
</rule>
<rule name="Friendly3" stopProcessing="true">
<match url="^(.+)/(.+)\.(.+)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.php/{R:2}.{R:3}" appendQueryString="false" />
</rule>
<rule name="Friendly4" stopProcessing="true">
<match url="^(.+)/(.+)$" ignoreCase="true"/>
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.php?c={R:2}" appendQueryString="false" />
</rule>
It works for the following cases:
www.domain.com/data -> www.domain.com/data/index.php
www.domain.com/data/ -> www.domain.com/data/index.php
www.domain.com/data/aaa -> www.domain.com/data/aaa.php
It doesn't work for the following cases:
www.domain.com/data/aaa/ -> www.domain.com/data/aaa.php
www.domain.com/data/aaa/chapter1 -> www.domain.com/data/aaa.php?c=chapter1
For which cases I get a "not found".
Am I doing something wrong? How can I solve this ?
IIS = 7.5+
Thank you in advance.
How about this...
<rewrite>
<rules>
<rule name="Friendly1" stopProcessing="true">
<match url="^data/([-a-zA-Z0-9]*)/[-a-zA-Z0-9]*)$" ignoreCase="true" />
<action type="Rewrite" url="data/{R:1}.php?c={R:2}" />
</rule>
<rule name="Friendly2" stopProcessing="true">
<match url="^data/([-a-zA-Z0-9][-a-zA-Z0-9]*)$" ignoreCase="true" />
<action type="Rewrite" url="data/{R:1}.php" />
</rule>
<rule name="Friendly3" stopProcessing="true">
<match url="^data(/.*)$" ignoreCase="true" />
<action type="Rewrite" url="data/index.php" />
</rule>
</rules>
</rewrite>

How to rewrite base path (i.e. "/") with IIS rewrite?

How do I rewrite site's base path (i.e. /) using IIS rewrite?
I've tried this so far:
<rule name="RewriteIndexUrl" stopProcessing="true">
<match url="/" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.html" />
</rule>
Currently it's not rewriting that base path, and it's returning the index.html directly.
I could figure out a possible solution and it worked:
Remove all default documents in IIS site (i.e. index.html, index.htm, default.aspx...)
Add the following rewriting rule:
<rule name="RewriteIndexUrl" stopProcessing="true">
<match url="^" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="false" />
</conditions>
<action type="Rewrite" url="index.html" />
</rule>

iis url rewrite user-friendly urls multiple rules conflict

I've been trying to create user-friendly urls for two of my cfm pages using url rewrite module on IIS 8. That said, there seems to be some sort of conflict due to similarity between rules or something.
Here is what I want to achieve. The following pages:
www.mywebsite.com/news.cfm?category=microsoft
www.mywebsite.com/news.cfm?category=apple
etc
www.mywebsite.com/blog.cfm?title=sometitle
www.mywebsite.com/blog.cfm?title=sometitle
etc
Should look like this:
www.mywebsite.com/news/microsoft
or this
www.mywebsite.com/microsoft
www.mywebsite.com/blog/sometitle
or this
www.mywebsite.com/sometitle
You get the picture.
Here is my code:
<rule name="RedirectUserFriendlyURL4" stopProcessing="true">
<match url="^news\.cfm$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^category=([^=&]+)$" />
</conditions>
<action type="Redirect" url="news/{C:1}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL4" stopProcessing="true">
<match url="^news/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="news.cfm?category={R:1}" />
</rule>
<rule name="RedirectUserFriendlyURL5" stopProcessing="true">
<match url="^blog\.cfm$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^title=([^=&]+)$" />
</conditions>
<action type="Redirect" url="blog/{C:1}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL5" stopProcessing="true">
<match url="^blog/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="blog.cfm?title={R:1}" />
</rule>
The rules above do not work together and both pages redirect to the one that's set in the first rule. Can this be done with a single rule or do I need to use two? How can I sort this out? I would really appreciate your help on this one. I am using ColdFusion 10 on Windows and IIS 8.

Friendly URL with the same pattern

I can redirect and make a single Friendly URL:
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^Product/Tour\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
</conditions>
<action type="Redirect" url="Product/Tour" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL2" stopProcessing="true">
<match url="^Product/Tour$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="Product/Tour.aspx" />
</rule>
But, for example, if I have:
http://www.domain.com/Product/Features.aspx
http://www.domain.com/Product/Download.aspx
http://www.domain.com/Product/FAQ.aspx
etc.
Can I write one rule to make friendly URL for all of these links in order to receive?
http://www.domain.com/Product/Features
http://www.domain.com/Product/Download
http://www.domain.com/Product/FAQ
It's easy when there is a couple of links, but with a lot of rules it's hard to maintain.
You can use the regex pattern and the back reference to do this:
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^Product/([A-z0-9]+)\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
</conditions>
<action type="Redirect" url="Product/{R:1}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL2" stopProcessing="true">
<match url="^Product/([A-z0-9]+)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="Product/{R:1}.aspx" />
</rule>
If you have a lot of urls to rewrite, you should read the documentation about the Rewrite Maps as well.

Resources