Как указать путь к файлу в web.config для проверки существования файла? - PullRequest
0 голосов
/ 03 октября 2019

Каталог файлов сайта выглядит следующим образом: enter image description here

В IIS. каталог веб-сайта установлен в C: \ mywebiste и связан с портом 8086. Следующая веб-страница может просматриваться без проблем:

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

Я хочу использовать IIS rewrite дляиспользуйте сжатый файл foo.dat ONLY , если он существует, поэтому файл 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>   

1 Ответ

2 голосов
/ 03 октября 2019

Если я вас правильно понял, вы всегда хотите переписать версию в папке _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>
...