Question

I'm struggling to transform an Apache .htaccess rewrite to an IIS rewrite. I would like to rewrite from one directory to another.

Example: If someone accesses the URI: /booklets/MyPDF.pdf then the rewrite would access the file using /res/pdf/MyPDF.pdf

This is what I have so far:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Rewrite for booklets location" enabled="true">
            <match url="^booklets/(.*).pdf" />
            <action type="Rewrite" url="res/pdf/{R:1}.pdf" />
        </rule>

      </rules>
    </rewrite>
  </system.webServer>
</configuration>

Any help would be greatly appreciated.

Was it helpful?

Solution

In addition to your own answer i would suggest you to add stopProcessing attribute and backslash dot symbol before extension in regular expression:

   <rule name="Rewrite for booklets location" stopProcessing="true">
      <match url="^booklets/(.*)\.pdf$" ignoreCase="true"/>
      <action type="Rewrite" url="res/pdf/{R:1}.pdf"/>
   </rule>

OTHER TIPS

After tinkering around with it, I just removed enabled="true" from the rule and added the ending regex $ to the url match, and now it works!

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Rewrite for booklets location">
            <match url="^booklets/(.*).pdf$" />
            <action type="Rewrite" url="res/pdf/{R:1}.pdf" />
        </rule>

      </rules>
    </rewrite>
  </system.webServer>
</configuration>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top