Search code examples
iisasp-classicweb-config

url rewriting in IIS causes css and images files not to get loaded in browser


I have tried to solve the issue of not getting css and images files loaded in browser after url rewriting in IIS for classic asp app. I have found the solution which shows in developer tool's network tab that files are loaded (Status Code: 200 OK) and css and image urls are now pointing to correct folders - but file contents don't just get loaded. Here is the web.config based on the solution (How to fix URL Rewriting for links inside CSS files with IIS7):

  <rule name="Rewrite css URLs" preCondition="IsCSS" stopProcessing="true">
      <match pattern="localhost/asp-app/data/styles" />
      
      <action type="Rewrite" value="localhost/asp-app/styles" />
  </rule>
  <preConditions>
      <preCondition name="IsCSS">
          <add input="{RESPONSE_CONTENT_TYPE}" pattern="text/css" />
      </preCondition>
  </preConditions>

    </outboundRules>

<rules>
        <rule name="Rewrite-to-http-verbs-handler">
            <match url=".*" />
            <action type="Rewrite" url="/asp-app/routes/router2.asp" />
        </rule>
    </rules>
</rewrite>

correct css-file path after rewrite rule

response header information is ok

And even though I use absolute url in the css-link tag in the head section

<link rel="stylesheet" href="http://localhost/asp-app/styles/test.css">,

the css don't not get loaded. And the same is with images, even full urls are not working, ie.

<img src="http://localhost/asp-app/images/konsultaatio.jpg" />

Any ideas how to fix?


Solution

  • I eventually found the solution for the issue. It is based on what is presented here: https://community.progress.com/s/article/Sitefinity-backend-not-working-correctly-when-using-rewrite-rules

    So, what was needed to fix the issue was to add a rule to prevent css, images, and also js -files to be loaded simultaneously with the asp-file rewriting. Note that the rule for the

    action type="None"

    has to precede the asp-file rewriting rule.

    <rewrite>
      <rules>
        <rule name="preventRewriteRulesFor" stopProcessing="true">
            <match url="^/?(styles|images|js)/" ignoreCase="true" />
            <action type="None" />
        </rule>
        <rule name="Rewrite" stopProcessing="true">
            <match url=".*" />
            <action type="Rewrite" url="/asp-app/routes/router2.asp" />
        </rule>
      </rules>
    </rewrite>
    

    I realized the solution when I saw in the network tab for the browser tools that css, js and even images response raw data was the very same as was the loaded html-content of the asp file. Thus, css, js and image files had to be prevented from being automatically rewritten along with the rule for asp-file.

    Therefore, also, I could skip the outboundRules and preConditions altogether given in the original question - they are not needed.