如何在 web.config 中指定文件路径来检查文件是否存在?

网站的文件目录如下所示: 在此处输入图片说明

在 IIS 中。网站目录设置为C:\mywebiste,绑定8086端口,可以正常浏览以下网页:

https://localhost:8086/home/dist/

我想使用 IIS 重写以使用压缩的 foo.dat(如果存在),因此 web.config 如下所示:

<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="{APPL_PHYSICAL_PATH}home\dist\_compressed_br\foo.dat" matchType="IsFile" negate="false" />
  </conditions>
  <action type="Rewrite" url="_compressed_br/foo.dat" />
</rule>

它工作正常。由于我可能将dist文件夹下的内容分发到任意目录下对应的web.config,想知道有没有参数可以代替"{APPL_PHYSICAL_PATH}home\dist"这样我就可以使用同一个web.config我把它们放在哪里。这个问题是根据善良答案提供者的建议另一个类似问题的扩展

[编辑] 2019-10-03基于优秀的评论答案,我现在可以重写所有文件:

<rule name="Rewrite br" stopProcessing="true">
  <match url="^(.*)$"/>
  <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
    <!-- Match brotli requests -->
    <add input="{HTTP_ACCEPT_ENCODING}" pattern="br" />
    <!-- following condition captures a group {C:1} with the value "C:\some\directory" for a path "c:\some\directory\foo.dat" -->
    <!-- so we can use in the next condition to check whether the compressed version exists -->
    <add input="{REQUEST_FILENAME}" pattern="^(.*)\\([^\\]*)$"/>
    <!-- Check if the pre-compressed file exists on the disk -->
    <!-- {C:1} used as requested file's parent folder -->
    <add input="{C:1}\_compressed_br\{C:2}" matchType="IsFile"/>
    </conditions>
  <action type="Rewrite" url="_compressed_br/{C:2}" />
  <serverVariables>
    <set name="RESPONSE_Content-Encoding" value="br"/>
  </serverVariables>
</rule>   
库尔蒂金

如果我理解正确,您总是想重写 .xml_compressed_br文件旁边文件夹下的版本web.config

如果是这样,请尝试以下操作:

<rule name="foo" stopProcessing="true">
    <match url="^foo\.dat$"/>
    <!-- trackAllCaptures="true" is required to capture regex groups across conditions -->
    <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
        <add input="{HTTP_ACCEPT_ENCODING}" pattern="br"/>
        <!-- following condition captures a group {C:1} with the value "C:\some\directory" for a path "c:\some\directory\foo.dat" -->
        <!-- so we can use in the next condition to check whether the compressed version exists -->
        <add input="{REQUEST_FILENAME}" pattern="^(.*)\\foo\.dat$"/>
        <!-- {C:1} used as requested file's parent folder -->
        <add input="{C:1}\_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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在gulp.config.js中指定文件?

如何添加嵌套的Web.config文件?

如何在web.config文件中为特定文件类型创建重定向?

如何在web.config文件中的Powershell中更新变化的值?

如何在Web.config文件中设置变量值

如何在没有web.config的情况下从Azure公开和提供WOFF文件?

如何在web.config转换过程中添加文件?

如何在.Net Core应用中读取web.config文件

如何在web.xml中的spring contextConfigLocation中同时指定spring java config和xml config

如何检查文件是否存在于 Laravel 6 的 config 文件夹中?

如何使用web.config文件强制HTTPS

如何通过代码修改web.config文件中的密钥?

如何设置web.config文件以显示完整的错误消息

web.config文件转换

加密web.config文件

如何在MVC项目的Web.Config文件中的身份验证标签内正确编写表单标签

如何在IIS虚拟文件夹而不是网站根目录中找到网站web.config

如何验证web.config

如何在Web.config中转换此元素?

如何在web.config中设置会话超时

如何在config.xml中指定Cordova Android版本?

如何在Typesafe Config中指定多行字符串?

我们如何使用WinRM更新web.config文件-IIS Web App部署任务

检查内容是否在web.config中

如何在 CSS 文件中指定图像的路径?

如何在Jupyter Notebook中指定文件路径

如何在 .bat 文件中指定目录路径?

如何在cmake中指定.lib文件的路径?

如何在属性文件中指定 chromedriver 路径