0

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?

PeanutsMonkey
  • 1,832
  • 8
  • 26
  • 27

2 Answers2

2

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).

LazyOne
  • 3,014
  • 1
  • 16
  • 15
  • 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. – PeanutsMonkey Jul 12 '11 at 22:21
  • @PeanutsMonkey See my updated answer. Useful link: http://learn.iis.net/page.aspx/734/url-rewrite-module/ – LazyOne Jul 12 '11 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 as `true` 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 to `false` I take it means that if the requested file or folder is indeed a file or folder. Is that also correct? – PeanutsMonkey Jul 12 '11 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 set `negate="false"` -- instead just do not use this param, i.e. ``; 4) As you can see there are separate checks for File and Directory. There is no `IsExist` 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). – LazyOne Jul 12 '11 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? – PeanutsMonkey Jul 12 '11 at 22:59
  • Now I'm confused. I'm not familiar with APS.NET that much and don't really know what do you mean by _"what if the file exists as a database created page"_. But yes -- by DOES exist I mean "File exist on File System". – LazyOne Jul 12 '11 at 23:02
  • Sorry LazyOne. What I mean is that if I have a page that was created by a CMS and it exists as a 'file' in the database. – PeanutsMonkey Jul 12 '11 at 23:07
  • Well .. these rules work with real files only. This sort of logic (file exist/absent in database) you have to check in your ASPX file. You also may do some routing using special .NET class methods (I don't know -- maybe `UrlRewritingNet.UrlRewrite` or something like that: http://haacked.com/archive/2008/03/11/using-routing-with-webforms.aspx ). – LazyOne Jul 12 '11 at 23:13
-2

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>