英文:
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>
<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>
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?
答案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.
<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.
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论