URL Rewriting the root folder to a subfolder

Re: URL Rewriting the root folder to a subfolder

Oct 10, 2009 09:44 PM

I’m actually using IIS7 Url Rewrite 1.1 but conceptually same thing..

I ran across this tidbit on http://learn.iis.net/page.aspx/460/using-url-rewrite-module/

“IIS DefaultDocument module may redirect to rewritten URL when URL rewrite module rewrites to a folder. For example, if substitution URL in rewrite rule is “/folder1/folder2” and these folders exist on file system and have default document configured, then DefaultDocument module will redirect web client to “/folder1/folder2/“, thus exposing rewritten URL. To prevent this from happening, use “/” at the end of the substitution URL when rewriting to a folder, e.g. “/folder1/folder2/“”

Which gave me the idea to adjust the Rewrite Url, and discovered its strange behavior.  Indeed, if you rewrite to a folder with a default document without the trailing slash it (erroneously?) exposes the rewritten url (as stated above).  Adding the trailing slash worked but oddly it then broke asp.net themes.  Finally adding the folder/default.aspx (or whatever your default document is) works as you would expect.

Also I did have to remove the condition that it not match on a directory.  Here is the final rule fyi.

<rule name="root" stopProcessing="true">
   <match url="^$" />
      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
   <action type="Rewrite" url="WEBAPP/default.aspx" />

<rule name="AddTrailingSlashRule1" stopProcessing="true">
   <match url="(.*[^/])$" />
      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
      <add input="{URL}" pattern="myBundle$" negate="true" />
   <action type="Redirect" url="{R:1}/" redirectType="Found" />

Simply add new {URL} lines for your exclusions. This example ‘ends with’ myBundle, but you could use the following instead:

<add input="{URL}" pattern="/bundles/" negate="true" />

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s