【问题标题】:URL Rewrite with Relative URLs使用相对 URL 重写 URL
【发布时间】:2018-08-25 00:46:24
【问题描述】:

我很可能会遗漏一些东西。我在这篇很棒的帖子中找到了这个 URL 重写示例:http://marisks.net/2017/05/14/changing-static-resource-urls-to-cdn-urls-with-url-rewrite/

我的设置是相同的。我的代码中有相对 URL,我想即时重写它们以指向我的 CDN。

<outboundRules>
  <clear />       
  <rule name="CDN" preCondition="CheckHTML" enabled="false" stopProcessing="true">
    <match filterByTags="Img, Link, Script, CustomTags" customTags="Video" pattern="(^(?!www\.|(?:http|ftp)s?:\/\/|[A-Za-z]:\\|\/\/).*\.(jpg|jpeg|png|js|css|mp4).*)" />
    <action type="Rewrite" value="http://cdn.example.com{R:1}" />
    <conditions logicalGrouping="MatchAll">
    </conditions>
  </rule>   
  <preConditions>
    <preCondition name="CheckHTML">
      <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
   </preCondition>
  </preConditions>
  <customTags>
    <tags name="Video">
      <tag name="source" attribute="src" />
    </tags>
  </customTags>           
</outboundRules>

这很好用。 除了我需要对特定文件夹的重写进行微调。 我知道这听起来很简单,但是当我查看上述脚本中的正则表达式时 - 我看不到在哪里可以对我的相对 URL 中的单词进行任何类型的限制。显然我需要在 Regex 上更加努力。但我们感谢您提供任何帮助。

我只需要捕获与某些文件夹的相关链接,例如 /media 或 /css(“/media/media.mp4”或“/css/base.css”)。

【问题讨论】:

    标签: asp.net iis url-rewriting url-rewrite-module


    【解决方案1】:

    我相信我解决了这个问题。至少,它适用于我设置为重写的文件夹并忽略我未指定的文件夹。我会做更多的测试,但现在可以了。

    <rule name="CDN" preCondition="CheckHTML" enabled="true" stopProcessing="true">
        <match filterByTags="Img, Link, Script, CustomTags" customTags="Video" pattern="(^(\/(media|css)+)*\/.*)" />
        <action type="Rewrite" value="http://cdn.example.com{R:0}" />
        <conditions logicalGrouping="MatchAll">
        </conditions>
    </rule> 
    

    前提条件和标签始终相同。

    更新了正则表达式:

    (^\/media\/.*)|(^\/css\/.*)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多