0

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:

enter image description here

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

Edit (2019-09-30): enter image description here

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.

Hong
  • 17,643
  • 21
  • 81
  • 142
  • you could try to match like: this server variable match The path-absolute part of the URI.example if you enter http://www.sample.com/s2 it will return /s2 . – Jalpa Panchal Sep 25 '19 at 07:49
  • @JanviPanchal Thank you for trying to help. I did exactly as you wrote, but it did not work. I also tried without the first forward slash: , it failed too. The test url is: "localhost/mywebapp/" Actually, I could download foo.dat with "localhost/mywebapp/_compressed_br/foo.dat", so your suggestion is supposed to work. I red somewhere the URL needs to be translated to a file path somehow. – Hong Sep 25 '19 at 21:19
  • could you share your folder structure and test url? and also try to use failed request tracing to get more detail why your rule is not working[link](https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/using-failed-request-tracing-to-trace-rewrite-rules). – Jalpa Panchal Sep 27 '19 at 08:17
  • you could refer this links on how to check file is exist or not by using url rewrite. [link1](https://forums.iis.net/t/1162295.aspx),[link2](https://stackoverflow.com/questions/7595782/iis-rewrite-rule-how-to-check-requesturl-php-file-is-exists-or-not-in-rule),[link3](https://superuser.com/questions/975054/try-to-rewrite-to-a-different-url-if-it-would-result-in-404) – Jalpa Panchal Sep 27 '19 at 08:18
  • @JalpaPanchal I added the information per your request. I enabled Failed Request tracing, but have not seen anything in the log. I suspect it is not considered as a failed request. – Hong Sep 27 '19 at 14:58
  • could you share failed request tracing section snapshot? – Jalpa Panchal Sep 30 '19 at 03:22
  • @JalpaPanchal I added the screenshot per your request. Thank you for your persistent help. – Hong Oct 01 '19 at 02:13

1 Answers1

2

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>
Kul-Tigin
  • 16,728
  • 1
  • 35
  • 64
  • Thank you again for the help. I am wondering if you could lend your capable hand again if you do not mind. Since it is hard to describe it in the comment here, I appended the new challenge to the original question. Could you take a look at it? If you think it is better to create another question, I will be happy to do that. – Hong Oct 03 '19 at 01:36
  • @Hong By addressing [this](https://meta.stackoverflow.com/questions/266767/what-is-the-the-best-way-to-ask-follow-up-questions) I suggest you to ask a new follow-up question with more details, web site root directory, web application's root directory etc. Besides there might be better solutions like one HTTP module file rather than several `web.config`s. – Kul-Tigin Oct 03 '19 at 02:25
  • Thank you. Will do tomorrow. Late night here. – Hong Oct 03 '19 at 02:52
  • I posted the new question here: https://stackoverflow.com/questions/58219097/how-to-specify-the-file-path-in-web-config-for-checking-file-existence – Hong Oct 03 '19 at 12:27