【问题标题】:URL Rewrite rule for IIS to replace folder path in everypageIIS 的 URL 重写规则以替换每个页面中的文件夹路径
【发布时间】:2016-02-17 18:21:37
【问题描述】:

我的网站项目中有 300 多个页面。随着时间的推移,我们创建了一个安全的新服务器。此服务器专门用于网站中的所有图片。

所以这里是场景:

  • 图像的当前实现(在 aspx 中,在 css 中)

    http://www.mysite.com/assets/common/image1.jpg
    
  • 有时在网页和 css 中是这样指定的

    ~/assets/common/image1.jpg        
    
  • 想用这样的东西。

    http://www.static.mysite.com/common/image1.jpg
    
  • 对于安全页面

    https://www.static.mysite.com/common/image1.jpg
    

如您所见,所有图像都来自~/assets 文件夹,但现在我想创建一个规则,将~/assets 替换为http://static.mysite.com

如何在 IIS 中使用重写规则来实现这一点。

示例:

ASPX

 <img src="/assets/common/image1.jpg" id="ImageId1" alt="Image" width="100" height="100" />

<img src="http://mysite.com/assets/common/image2.jpg" id="ImageId2" alt="Image" width="100" height="100" />

想要有IIS规则,找到上面的代码,替换成http://static.mysite.com/common/image1.jpg

 <img src="http://static.mysite.com/common/image1.jpg" id="ImageId1" alt="Image" width="100" height="100" />


<img src="http://static.mysite.com/common/image2.jpg" id="ImageId2" alt="Image" width="100" height="100" />

【问题讨论】:

    标签: asp.net iis url-rewriting


    【解决方案1】:

    您需要在 IIS 中创建出站规则。规则将需要以下内容:

    1. 前提条件应该只检查 html 文件(我使用默认的 IsHTML)
    2. 在“匹配内容”中选择您要检查链接的元素
    3. 模式为^(.*)/assets/(.*)
    4. 操作属性是http://static.mysite.com/{R:2}。 R:2 指的是上面正则表达式中的第二个()。点击“测试模式”按钮后,您可以查看您需要的内容。

    满足上述条件的简单规则:

    【讨论】:

    • 如何使用web.Config 进行配置,我希望Content 文件夹中的所有Images 文件夹、cssjs 都指向Azure CDN?
    • @stom 以上所有更改都在 web.config 中可见,因此您可以复制粘贴它们
    【解决方案2】:

    你可以试试这个

    <rule name="assets redirection" stopProcessing="false">
        <match url="^(.*)/(assets)/(.*)" ignoreCase="false" />
        <action type="Redirect" url="{R:1}/{R:3}" />
    </rule>
    

    它将whatever/assets/common/image1.jpg重定向到whatever/common/image1.jpg

    更新:

    <rule name="assets redirection" stopProcessing="false">
        <match url="^(.*)/(assets)/(.*)" ignoreCase="false" />
        <action type="Redirect" url="static.mysite.com/{R:3}" />
    </rule>
    

    【讨论】:

    • 我希望它通过替换 mysite.com/assets 重定向到 static.mysite.com/common/image1.jpg
    • 我正在使用 godaddy 进行托管。那么我应该将上面的重写放在单个网站的虚拟目录/文件夹中还是放在根目录中?
    • @SnehaJavalkar,取决于您要应用规则的级别。您也可以在根级别定义它
    【解决方案3】:

    嗯,你怎么能这样做

    string imageUrl= Request.Url.Scheme + "://" + Request.Url.Authority + "/Image/logo/Logo.png";
    

    Scheme 是你的 web Schemes http 或 https 等。 授权是您的网络域名和端口(如果有)。 然后这里就完全是你的图片网址了。

    我已将此徽标 url 用于 ssrs 报告服务并且很好。希望它对你有用。

    欢迎评论和询问。 谢谢。

    【讨论】:

      猜你喜欢
      • 2020-02-17
      • 2019-08-22
      • 2018-09-18
      • 2015-08-26
      • 1970-01-01
      • 2011-07-18
      • 2015-08-17
      • 2014-04-08
      • 2023-03-26
      相关资源
      最近更新 更多