0

A website's file directory looks like the following: enter image description here

In IIS. the website's directory is set to C:\mywebiste, and is bound port 8086. The following web page can be browsed without any problem:

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

I want to use IIS rewrite to use the compressed foo.dat ONLY if it exists, so the web.config looks like the following:

<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>

It works fine. Since I may distribute 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 place them. This question is an extension from another similar question per the suggestion of the kind answer provider.

[Edit] 2019-10-03 Based on the excellent well commented answer, I can rewrite for all the files now:

<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>   
Kul-Tigin
  • 16,728
  • 1
  • 35
  • 64
Hong
  • 17,643
  • 21
  • 81
  • 142

1 Answers1

2

If I understood you correctly, you always want to rewrite the version under the _compressed_br folder next to the web.config.

If so, give the following a try:

<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>
Kul-Tigin
  • 16,728
  • 1
  • 35
  • 64
  • Thanks a lot. It works like a charm again. I actually want to do this for all the files that need to be handled this way. Your well commented answer provides a solid foundation for me to achieve that. – Hong Oct 03 '19 at 20:35
  • 1
    I'm glad I could help @Hong Good luck. – Kul-Tigin Oct 03 '19 at 20:36
  • Many thanks again. I can rewrite for all the files now based on your answer. I appended my final rule to my post. Hope I am not fooling myself after repeated testing with success. – Hong Oct 03 '19 at 21:40
  • Thank you for the confirmation. This will be used for many web apps. – Hong Oct 03 '19 at 22:32
  • 1
    I see. In case you have your own web server don't know about it, there's an extension published by Microsoft that adds support for Brotli compression to your IIS installation. https://www.iis.net/downloads/microsoft/iis-compression Keep that in mind too. – Kul-Tigin Oct 03 '19 at 22:41
  • Thank you for the information. Yes, I have 100% control of the server. Would using pre-compressed files be slightly faster than compression at run time? I have not idea how the extension work. I imagine it compresses file while they are requested. – Hong Oct 03 '19 at 23:29
  • 1
    @Hong Serving pre-compressed files will of course be faster at first but it comes with its own costs. Manpower to create files, folders, and configs, making tests, asking quesitons on SO ;), the cost of Url Rewrite Module's itself, and so on. The extension have a good compressed file caching mechanism with the power of IIS' kernel mode caching capabilities. So if things grow in the future, consider to use the extension. – Kul-Tigin Oct 04 '19 at 00:19
  • Excellent points. The compressed files and folder structure are generated automatically by a web app build process. Unfortunately, the provided rewrite rules do not work. Otherwise, I would never have looked this, let alone investigating this. If I encounter any more problems of using the pre-compressed files, I will give up on it, and try the extension. – Hong Oct 04 '19 at 00:45
  • Hi @Kul-Tigin, I'm the original author of the rewrite rule, and the pre-compression, as hong said it is indeed automatically generated. The biggest reason for using this technique instead of using the IIS provided compression is the compression time itself. It's *very* slow for large files (e.g. 30MB+), and unreliable for cold startup. In case you ask, 30MB+ files are WebAssembly files. – Jérôme Laban Oct 25 '19 at 15:43
  • 1
    @JeromeLaban I'm positive with your solution, and find it useful. I'd the do same in your case. – Kul-Tigin Oct 25 '19 at 15:57
  • @Kul-Tigin sorry for bothering you. I finally took your suggestion to use IIS compression. I have been struggling to make it work. Could you take a look at my new question if you do not mind? https://stackoverflow.com/q/63088626/355456 – Hong Jul 25 '20 at 13:03
  • @Hong Hi. I'll be available tomorrow then I'll definitely take a look at the question. – Kul-Tigin Jul 26 '20 at 00:36
  • @Kul-Tigin Uno has changed the packaging, so the old rewrite rules stopped working. This is why I wanted to take your suggestion to solve this problem once for all. I have to say that I have made new rules based on the excellent explanation that you provided, they are working now. Now, I have a fairly good understanding of the few rules that I need to know thanks to your tips. – Hong Jul 26 '20 at 01:12