如何检查子文件夹中的文件以进行IIS重写? [英] How to check a file in a sub-folder for IIS Rewrite?

查看:49
本文介绍了如何检查子文件夹中的文件以进行IIS重写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

web.config中用于重写的以下内容工作正常:

 <规则名称="foo" stopProcessing ="true">< match url ="foo.dat $"/><条件><!-匹配brotli请求-><添加输入="{HTTP_ACCEPT_ENCODING}" pattern ="br"/></conditions>< action type ="Rewrite" url ="_ compressed_br/foo.dat"/></rule> 

我想添加一个条件以确保仅在子文件夹中的压缩文件存在的情况下才进行重写:

 <规则名称="foo" stopProcessing ="true">< match url ="foo.dat $"/><条件><!-匹配brotli请求-><添加输入="{HTTP_ACCEPT_ENCODING}" pattern ="br"/><!-检查磁盘上是否存在预压缩文件-><添加输入="{DOCUMENT_ROOT}/_ compressed_br/foo.dat" matchType ="IsFile" negate ="false"/></conditions>< action type ="Rewrite" url ="_ compressed_br/foo.dat"/></rule> 

该条件永远不会发生重写.这意味着检查总是返回false.我也尝试了以下方法以使该条件无济于事:

 <添加输入="{DOCUMENT_ROOT} _compressed_br/foo.dat" matchType ="IsFile" negate ="false"/>< add input ="/_ compressed_br/foo.dat" matchType ="IsFile" negate ="false"/><添加input ="_ compressed_br/foo.dat" matchType ="IsFile" negate ="false"/> 

任何人都可以给这个小费吗?

编辑(2019-09-27):文件夹结构:

Web应用程序foo的目录为... \ dist.打开Web应用程序的URL是:

编辑(2019-10-01):

被接受的答案就像上述问题的魅力一样.

我有一个新的挑战.如果我将Web文件放在以下目录中:C:\ mywebsite \ home \ dist \ web.config

该网站绑定到端口8086.我可以浏览以下网页: https://localhost:8086/home/dist/

要使重写生效,我将必须使用以下内容:

 <添加输入="{APPL_PHYSICAL_PATH} home \ dist \ _compressed_br \ foo.dat" matchType ="IsFile" negate ="false"/> 

由于我可以将内容放置在目录dist下的相应目录下的web.config中,因此我想知道是否有一个参数可以替换"{APPL_PHYSICAL_PATH} home \ dist",以便可以使用同一网站.配置,无论我把它们放在哪里.

解决方案

您可以使用 {APPL_PHYSICAL_PATH} 查找Web应用程序 foo 的根文件夹.

设置响应头 Content-Encoding:br 可能还需要防止意外行为,例如 foo.dat 的文件下载对话框,而不是显示其解码后的响应

所以这是您需要的规则:

 <代码><规则名称="foo" stopProcessing ="true">< match url ="^ foo \ .dat $"/>< conditions logicalGrouping ="MatchAll" trackAllCaptures ="false"><添加输入="{HTTP_ACCEPT_ENCODING}" pattern ="br"/><!-{APPL_PHYSICAL_PATH}等于设置中的{DOCUMENT_ROOT} +"dist \"-><添加输入="{APPL_PHYSICAL_PATH} _compressed_br \ foo.dat" matchType ="IsFile"/></conditions>< action type ="Rewrite" url ="_ compressed_br/foo.dat"/>< serverVariables>< set name ="RESPONSE_Content-Encoding" value ="br"/></serverVariables></rule> 

The following in web.config for rewrite works fine:

<rule name="foo" stopProcessing="true">
  <match url="foo.dat$"/>
  <conditions>
    <!-- Match brotli requests -->
    <add input="{HTTP_ACCEPT_ENCODING}" pattern="br" />
  </conditions>
  <action type="Rewrite" url="_compressed_br/foo.dat" />
</rule>

I want to add a condition to make sure the rewriting is done only if the compressed file in the sub-folder exists:

<rule name="foo" stopProcessing="true">
  <match url="foo.dat$"/>
  <conditions>
    <!-- Match brotli requests -->
    <add input="{HTTP_ACCEPT_ENCODING}" pattern="br" />
    <!-- Check if the pre-compressed file exists on the disk -->
    <add input="{DOCUMENT_ROOT}/_compressed_br/foo.dat" matchType="IsFile" negate="false" />
  </conditions>
  <action type="Rewrite" url="_compressed_br/foo.dat" />
</rule>

The rewriting never happens with the condition. This means the checking always returns false. I have also tried the following for the condition to no avail:

<add input="{DOCUMENT_ROOT}_compressed_br/foo.dat" matchType="IsFile" negate="false" />
<add input="/_compressed_br/foo.dat" matchType="IsFile" negate="false" />
<add input="_compressed_br/foo.dat" matchType="IsFile" negate="false" />

Could you anyone offer a tip on this?

Edit (2019-09-27): Folder structure:

Web app foo's directory is ...\dist. The URL to open the web application is: http://localhost/foo/

Edit (2019-09-30):

Edit (2019-10-01):

The accepted answer works like a charm for the above problem.

I have a new challenge. If I put the web file in the following directory: C:\mywebsite\home\dist\web.config

The website is bound to port 8086. I can browse the following web page: https://localhost:8086/home/dist/

To make the rewrite work, I would have to use the following:

 <add input="{APPL_PHYSICAL_PATH}home\dist\_compressed_br\foo.dat" matchType="IsFile" negate="false" />

Since I may put the contents under folder dist with the corresponding web.config in any directory, I am wondering if there is a parameter that can replace "{APPL_PHYSICAL_PATH}home\dist" so that I can use the same web.config no matter where I put them.

解决方案

You can use {APPL_PHYSICAL_PATH} to locate your Web app foo's root folder.

Setting a response header Content-Encoding: br may also be required to prevent unexpected behaviors such as a file download dialog for foo.dat rather than displaying its decoded response.

So here's the rule you need:

<rule name="foo" stopProcessing="true">
    <match url="^foo\.dat$" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_ACCEPT_ENCODING}" pattern="br" />
        <!-- {APPL_PHYSICAL_PATH} equals to {DOCUMENT_ROOT} + "dist\" in your setup -->
        <add input="{APPL_PHYSICAL_PATH}_compressed_br\foo.dat" matchType="IsFile" />
    </conditions>
    <action type="Rewrite" url="_compressed_br/foo.dat" />
    <serverVariables>
        <set name="RESPONSE_Content-Encoding" value="br" />
    </serverVariables>
</rule>

这篇关于如何检查子文件夹中的文件以进行IIS重写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆