We have all of the web pages in our site under a /pub/src/
directory off the root of our site. I need a rewrite rule to leave the URL alone, but open the corresponding file in the /pub/src/
directory. For example:
Here's what I have so far:
<rule name="RewriteASPFiles" stopProcessing="true">
<match url="^(.+\.asp)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="/pub/src/{R:1}" />
</rule>
The regex captures the folder path and file name. But while /index.asp
correctly opens /pub/src/index.asp
, /login/index.asp
is giving a 404 - file not found. Also the default document is not captured by the rule. What is this missing to get my 2nd and 3rd examples above to work as well?
I tested your rewrite rule and here are the results:
1./index.asp
can correctly opens /pub/src/index.asp
2./login/index.asp
is giving a HTTP Error 404.0 - Not Found.
Then I enabled failed request tracing and got the following trace log.
You can see that this condition in the rule is not matched successfully. It checks whether the requested file exists, and the condition is considered satisfied only if the requested file does exist.
<add input="{REQUEST_FILENAME}" matchType="IsFile" />
Your request URL is /login/index.asp
, and the physical path corresponding to the file (C:\inetpub\wwwroot\login\index.asp) does not exist, so it is not satisfied.
If you remove the condition in the rule, you can satisfy your 1nd and 2rd examples.
3.For your 3rd example, sitename.com
-> opens -> sitename.com/pub/src/index.asp
. It doesn't match your existing rule, so you need to create another one.
Please try the following modified rewrite rules, which will make all three of your examples work.
<rewrite>
<rules>
<!-- Rule 1: Match URL path ending with .asp -->
<rule name="RewriteASPFiles" stopProcessing="true">
<match url="^(.+\.asp)$" />
<action type="Rewrite" url="/pub/src/{R:1}" />
</rule>
<!-- Rule 2: Match empty path -->
<rule name="MatchEmptyPath" stopProcessing="true">
<match url="^$" />
<action type="Rewrite" url="/pub/src/{R:0}" />
</rule>
</rules>
</rewrite>