【问题标题】:setting up cookieless domain for static resources为静态资源设置无 cookie 域
【发布时间】:2011-05-21 02:50:42
【问题描述】:

我正在使用 .net 3.5 在 IIS7 上运行一个 asp.net Web 应用程序。

为了提高我的 Yslow 分数,我正在考虑为我的静态资源(例如图像、CSS 和 JavaScript)实现一个无 cookie 域。

我的网站网址是 www.mywebsite.com。

因此,例如静态资源的 URL 为 static.mywebsite.com/styles.css

我想让这种变化尽可能无缝。我在整个站点中使用相对路径。

我可以设置子目录 static.mywebsite.com

但我还需要对我的应用程序进行更改。我正在寻求帮助。使用可包含在 web.config 中用于 URL 重写的新功能。关于如何为图像/css/javascript 设置 static.mywebsite.com 的任何提示或想法?

【问题讨论】:

  • 好问题,但恕我直言,您不应该这样做来提高您的 yslow 分数。只有当你相信你会有性能改进时,我才会这样做。用户不会关心你的 yslow 分数是多少。他们会想要一个快速的页面。当然,分数与速度之间存在相关性,但不要仅仅为了提高分数而做某事。 :)
  • 感谢您的评论 Joe,但我这样做的主要原因是为了提高网站的性能。我将不胜感激任何帮助或反馈,因为我真的很想尽快完成。

标签: asp.net web-config url-rewriting yslow cookieless


【解决方案1】:

可以使用出站规则。此规则会将 js、css、jpg 和 png 重写为 static.mywebsite.com。

<outboundRules rewriteBeforeCache="true">
    <rule name="CDN-01-css" preCondition="CheckHTML" stopProcessing="true">
      <match filterByTags="Link" pattern="/(.*\.css)" />
      <action type="Rewrite" value="http://static.mywebsite.com/{R:1}" />
    </rule>
    <rule name="CDN-01-js" preCondition="CheckHTML" stopProcessing="true">
      <match filterByTags="Script" pattern="/(.*\.js)" />
      <action type="Rewrite" value="http://static.mywebsite.com/{R:1}" />
    </rule>
    <rule name="CDN-01-jpg" preCondition="CheckHTML" stopProcessing="true">
      <match filterByTags="Img" pattern="/(.*\.jpg)" />
      <action type="Rewrite" value="http://static.mywebsite.com/{R:1}" />
    </rule>
    <rule name="CDN-01-png" preCondition="CheckHTML" stopProcessing="true">
      <match filterByTags="Img" pattern="/(.*\.png)" />
      <action type="Rewrite" value="http://static.mywebsite.com/{R:1}" />
    </rule>
    <preConditions>
      <preCondition name="CheckHTML">
        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
      </preCondition>
    </preConditions>
</outboundRules>

例如:

它会自动改变你的输出 html

&lt;link rel='stylesheet' id='social-logos-css' href='/wp-content/plugins/jetpack/_inc/social-logos/social-logos.min.css?ver=1' type='text/css' media='all' /&gt;

&lt;link rel='stylesheet' id='social-logos-css' href='http://static.mywebsite.com/wp-content/plugins/jetpack/_inc/social-logos/social-logos.min.css?ver=1' type='text/css' media='all' /&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-04
    • 2011-08-29
    • 2016-10-12
    • 1970-01-01
    • 2016-08-18
    • 1970-01-01
    • 2015-01-22
    • 2015-01-11
    相关资源
    最近更新 更多