简体   繁体   English

如何检查 IIS Rewrite 子文件夹中的文件?

[英]How to check a file in a sub-folder for IIS Rewrite?

The following in web.config for rewrite works fine : web.config 中的以下用于重写工作正常

<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.这意味着检查总是返回 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:编辑(2019-09-27):文件夹结构:

在此处输入图像描述

Web app foo's directory is...\dist. Web app foo 的目录是...\dist。 The URL to open the web application is: http://localhost/foo/ URL打开web应用是: http://localhost/foo/

Edit (2019-09-30):编辑(2019-09-30): 在此处输入图像描述

Edit (2019-10-01):编辑(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如果我将 web 文件放在以下目录中: 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/网站绑定8086端口,可以浏览如下web页面: 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.由于我可以将文件夹 dist 下的内容与相应的 web.config 放在任何目录中,我想知道是否有一个参数可以替换“{APPL_PHYSICAL_PATH}home\dist”,以便我可以使用相同的 web.config我把它们放在哪里。

You can use {APPL_PHYSICAL_PATH} to locate your Web app foo 's root folder.您可以使用{APPL_PHYSICAL_PATH}找到您的 Web app foo的根文件夹。

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.设置响应 header Content-Encoding: br可能还需要防止意外行为,例如foo.dat的文件下载对话框,而不是显示其解码响应。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM