I have written a list of regular expressions that currently redirect unused pages to the appropriately used pages e.g. if someone were to request abc.aspx it redirects them to home.aspx in IIS however seeing that I cannot account for all pages is there a way to capture a page that I may have missed and map it to another page?
2 Answers
Using URL Rewrite module v2:
<rule name="CatchAll" 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="/catchall.aspx?page={REQUEST_URI}" />
</rule>
This rule will catch ALL requests for non-existing files and directories. Usually you will place it at the end (the last or so).
All of such requests will be redirected (internally, of course) to /catchall.aspx
file, the requested URL will be in page
query string parameter. For example, if this page (http://www.example.com/hello-kitten
) will be routed through such catch-all file it will be rewritten to /catchall.aspx?page=/hello-kitten
.
{REQUEST_FILENAME}
: The full local filesystem path to the file or script matching the request. For example:
- Website root:
D:\websites\mysite.com\
- Requested URL:
http://mysite.com/help/delivery
{REQUEST_FILENAME}
=D:\websites\mysite.com\help\delievry
(even if such file/folder does not exist) -- I'm sure you can figure out how it is built from the above example (if virtual folders get involved then it will work a bit differently .. but the general idea is here).
The conditions for this rule, if translated to English, would be: if requested resource is NOT a file AND is NOT a folder, then conditions are met.
URL Rewrite module has IsFile
match type, but has no IsNotFile
. To "simulate" IsNotFile
the IsFile
is used in conjunction with negate="true"
. The same goes for IsDirectory
.
If you access URL Rewrite module via IIS Manager, and click "Edit" on this rule, you will have much easier time with understanding what they do exactly compared to my mumbling (IIS Manager has very good GUI for creating these rules).
-
Thanks Lazy One. Would you mind elaborating on the input types e.g.
{REQUEST_FILENAME}
as well as the matchtypes including the negate values? For example what they are, when I should use them and what they do. Jul 12, 2011 at 22:21 -
@PeanutsMonkey See my updated answer. Useful link: learn.iis.net/page.aspx/734/url-rewrite-module– LazyOneJul 12, 2011 at 22:41
-
That's fantastic. Thanks for elaborating on these. To clarify my understanding so that I am not confused. If I use the matchtype
IsFile
and set the negate value astrue
I am effectively saying if the request is NOT a file. If that is correct, I am confused by how would a requested file e.g. example.aspx be treated? Also just to be clear if I set the value of negate tofalse
I take it means that if the requested file or folder is indeed a file or folder. Is that also correct? Jul 12, 2011 at 22:44 -
1) Yes; 2)
/example.aspx
if such file DOES exist but condition says "Is Not File", then condition is not met and rewrite will not occur; 3) I actually do not know if you can setnegate="false"
-- instead just do not use this param, i.e.<add input="{REQUEST_FILENAME}" matchType="IsFile" />
; 4) As you can see there are separate checks for File and Directory. There is noIsExist
match type (it is all done similarly to Apache's mod_rewrite, which is a good thing as existing rules can be easily transferred/transformed into URL Rewrite rules).– LazyOneJul 12, 2011 at 22:56 -
Thanks LazyOne. I am confused by what you mean by if the file DOES exist? Do you mean if it exists physically on the file system? If yes, what if the file exists as a database created page? Jul 12, 2011 at 22:59
Wow, let's give that config a little more context!!
<configuration>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".json" mimeType="text/json" />
</staticContent>
<rewrite>
<rules>
<rule name="CatchAll" 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>
</rules>
</rewrite>
</system.webServer>
</configuration>