我可以为IIS虚拟目录创build一个“例外”吗?

我的服务器上有一个文件夹,像这样:

C:\的Inetpub \ wwwroot的\等等

在IIS中,我还设置了一个“/ blah”虚拟文件夹,指向C:\ BLAH

当然,当网站访问者inputhttp://www.myserver.com/blah/test.html时 ,IIS会查找C:\ BLAH \ test.html,而不是C:\ inetpub \ wwwroot \ blah \ test.html。 大多数情况下,这是我想要的行为,因为大部分文件都在C:\ BLAH中。 但是,当文件存在于C:\ inetpub \ wwwroot \ blah中时,我希望服务器首先在那里查找它,然后在C:\ BLAH中查找它是否找不到它。 基本上我想在C:\ inetpub \ wwwroot \ blah中的文件是虚拟目录的“例外”。 让服务器返回该文件夹中的文件(如果存在的话),但是如果它们不从C:\ BLAH中取出而不是返回404。

我在Windows Server 2000上运行IIS 5.0。

这可能吗? 谢谢。

这是一个贫民窟,但它可能是一个简单的方法来解决你的问题。

  • 创build一个指向c:\ blah的第二个虚拟目录。
  • 在“blah”虚拟目录(指向C:\ inetpub \ wwwroot \ blah)中,为c:\ inetpub \ wwwroot \ blah \中的ASP或ASP.Net页面设置404错误的自定义error handling程序。
  • 在404处理程序的ASP.Net页面中,
    • 拉请求的URL(它应该是唯一的东西在Request.QueryString)
    • 砍掉一切,但文件名要求
    • redirect到“/ cblah / FILENAME”。

下面是一些可以作为404处理程序的ASP.Net代码片段:

string qs = Page.Request.QueryString.ToString(); qs = Server.UrlDecode(qs); int c = qs.LastIndexOf("/"); string filepath = qs.Substring(c+1); Response.Redirect("/cblah/"+filepath); 

你也许可以通过一个自定义的书写ISAPIfilter来做到这一点,但是我不知道IIS或现有产品中的任何方法都能完全符合你的要求。

IIRF是一个免费的URL重写器,它能够在重写之前检查目录是否存在。 规则将如下所示:

 # This rule matches, eg, http://server/blah/test.html # It does "no rewrite" (-) if the file c:\blah\test.html does NOT exist. # The url stub /blah must map to an existing vdir. RewriteCond c:\blah\$1 !-f RewriteRule ^/blah/(.*)$ - [L] # If the file does exist, rewrite to the alternative vdir. RewriteCond c:\blah\$1 -f RewriteRule ^/blah/(.*)$ /override-vdir/$2 [L] 

你也可以通配符“blah”。 要做到这一点,你需要一个约定来命名覆盖vdirs。 它看起来像这样:

 # This rule matches, eg, http://server/blah/test.html # It does nothing if the file c:\blah\test.html does not exist. RewriteCond c:\$1\$2 !-f RewriteRule ^/([^/]+)/(.*)$ - [L] RewriteCond c:\$1\$2 -f RewriteRule ^/([^/]+)/(.*)$ /override-$1/$2 [L] 

还有testing目录存在的条件。 检查IIRF文档的细节。