IIS中的URL重写导致CSS和图像文件无法在浏览器中加载。

huangapple go评论49阅读模式
英文:

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

问题

我已经尝试解决在IIS中进行URL重写后,在经典ASP应用程序中无法加载CSS和图像文件的问题。我已经找到了解决方案,它在开发者工具的网络选项卡中显示文件已加载(状态码:200 OK),并且CSS和图像URL现在指向了正确的文件夹 - 但文件内容并没有加载。以下是基于该解决方案的web.config文件(https://stackoverflow.com/questions/8653399/how-to-fix-url-rewriting-for-links-inside-css-files-with-iis7):

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

即使在头部部分的CSS链接标签中使用了绝对URL:

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

CSS仍然不会加载。同样的情况也适用于图像,即使使用完整的URL也不起作用,例如:

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

有什么方法可以修复这个问题吗?

英文:

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 (https://stackoverflow.com/questions/8653399/how-to-fix-url-rewriting-for-links-inside-css-files-with-iis7):

<rewrite>
<outboundRules>

  &lt;rule name=&quot;Rewrite css URLs&quot; preCondition=&quot;IsCSS&quot; stopProcessing=&quot;true&quot;&gt;
      &lt;match pattern=&quot;localhost/asp-app/data/styles&quot; /&gt;
      
      &lt;action type=&quot;Rewrite&quot; value=&quot;localhost/asp-app/styles&quot; /&gt;
  &lt;/rule&gt;
  &lt;preConditions&gt;
      &lt;preCondition name=&quot;IsCSS&quot;&gt;
          &lt;add input=&quot;{RESPONSE_CONTENT_TYPE}&quot; pattern=&quot;text/css&quot; /&gt;
      &lt;/preCondition&gt;
  &lt;/preConditions&gt;

	&lt;/outboundRules&gt;

&lt;rules&gt;
		&lt;rule name=&quot;Rewrite-to-http-verbs-handler&quot;&gt;
			&lt;match url=&quot;.*&quot; /&gt;
			&lt;action type=&quot;Rewrite&quot; url=&quot;/asp-app/routes/router2.asp&quot; /&gt;
		&lt;/rule&gt;
	&lt;/rules&gt;
&lt;/rewrite&gt;

IIS中的URL重写导致CSS和图像文件无法在浏览器中加载。

IIS中的URL重写导致CSS和图像文件无法在浏览器中加载。

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

&lt;link rel=&quot;stylesheet&quot; href=&quot;http://localhost/asp-app/styles/test.css&quot;&gt;,

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

&lt;img src=&quot;http://localhost/asp-app/images/konsultaatio.jpg&quot; /&gt;

Any ideas how to fix?

答案1

得分: 0

我最终找到了解决方案。它基于这里提供的内容:https://community.progress.com/s/article/Sitefinity-backend-not-working-correctly-when-using-rewrite-rules

所以,需要修复问题的方法是添加一个规则,以防止同时加载css、图片和js文件与asp文件重写。请注意,action type="None" 的规则必须在asp文件重写规则之前。

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

当我在浏览器工具的网络选项卡中看到css、js甚至图片响应的原始数据与加载的asp文件的html内容完全相同时,我意识到了解决方案。因此,必须防止自动重写css、js和图像文件,以及asp文件的规则。

因此,原始问题中提到的outboundRules和preConditions都可以跳过 - 它们是不必要的。

英文:

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.

&lt;rewrite&gt;
  &lt;rules&gt;
	&lt;rule name=&quot;preventRewriteRulesFor&quot; stopProcessing=&quot;true&quot;&gt;
  		&lt;match url=&quot;^/?(styles|images|js)/&quot; ignoreCase=&quot;true&quot; /&gt;
		&lt;action type=&quot;None&quot; /&gt;
	&lt;/rule&gt;
	&lt;rule name=&quot;Rewrite&quot; stopProcessing=&quot;true&quot;&gt;
		&lt;match url=&quot;.*&quot; /&gt;
		&lt;action type=&quot;Rewrite&quot; url=&quot;/asp-app/routes/router2.asp&quot; /&gt;
	&lt;/rule&gt;
  &lt;/rules&gt;
&lt;/rewrite&gt;

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.

答案2

得分: -1

当您直接在浏览器中访问重写后的 URL 时,文件是否可以正常加载?您需要检查 CSS 文件和图像所在的文件夹是否具有访问权限,以确保 IIS 用户可以正常读取文件。

英文:

When you directly access the rewritten URL in the browser, can the file be loaded normally? You need to check whether the folders where the CSS files and images are located have access permissions to ensure that the IIS user can read the files normally.

huangapple
  • 本文由 发表于 2023年6月2日 08:54:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386546.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定