【问题标题】:IIS 8.0 Directory HttpRedirectIIS 8.0 目录 HttpRedirect
【发布时间】:2013-02-20 14:16:33
【问题描述】:

IIS 中有没有办法重定向以下请求: http://mysite/report1/img/logo.pnghttp://mysite/myapplication/report1/images/logo.png 用于 img 目录中的所有图像,而无需单独显式映射它们?

附加要求——我在映射到“report1”虚拟目录的驱动器上有数千个报告——每个都有自己的“img”目录——因此也没有合理的方法可以使用 IIS 管理器单独映射这些目录。

我正在寻找是否有某种方法可以在 IIS 服务器 web.config 文件中添加通配符(或其他)HttpRedirect 以正确映射所有报告的所有图像。我试过了:

<add wildcard="*res/img/" destination="/reporter/content/images/reportimages" />

但这似乎没有任何效果。

编辑:更多研究表明,使用 URL Rewrite 模块可能有效......但到目前为止我还没有让它工作。

我的规则如下所示(在 web.config 中):

<rules>
    <rule name="Redirect rule1 for ImageRedirect">
        <match url=".*" />
            <conditions>
                <add input="{ImageRedirect:{REQUEST_URI}}" matchType="Pattern" pattern="/res/img/(.+)" ignoreCase="true" negate="false" />
            </conditions>
            <action type="Redirect" url="{HTTP_HOST}/reporter/content/reporterimages/{C:1}" appendQueryString="false" />
     </rule>
</rules>

【问题讨论】:

    标签: url-rewriting web-config http-redirect url-rewrite-module iis-8


    【解决方案1】:

    您在使用 URL 重写模块时走在了正确的轨道上。
    在您的情况下,最简单的规则是:

    <rule name="Rewrite images" stopProcessing="true">
      <match url="^/report1/img/(.+)$" />
      <action type="Rewrite" url="/myapplication/report1/images/{R:1}" />
    </rule>
    

    它会检查请求的 url 是否与 ^/report1/img/(.+)$ 匹配,如果是,则触发对新文件夹的重写。

    如果您想改用重定向:

    <rule name="Redirect images" stopProcessing="true">
      <match url="^/report1/img/(.+)$" />
      <action type="Redirect" url="/myapplication/report1/images/{R:1}" />
    </rule>
    

    (如果不指定,默认情况下重定向是永久的(301))

    【讨论】:

    • 感谢您的回复!但我似乎无法让它工作。这是一个真实的输入 url:http://localhost/atlas/report-1.0.2/res/img/ctc.png 和重定向 url:http://localhost/reporter/content/images/reportimages/ctc.png。好像什么都没发生……?
    • 您是否将正则表达式更改为url="^atlas/report-1.0.2/res/img/(.+)$"?您是否使用过 Rewrite 或 Redirect 版本?
    • 我没有更改正则表达式,因为 URL 的 report-1.0.2 部分会更改。 url 唯一一致的部分是主机 (http://localhost) 和 /res/img/。我想我只需要将匹配更改为“/res/img/(.+)$”而不是开头 (^)。它似乎适用于一个特定的图片网址,但不适用于嵌入页面中的图片网址......仍在努力!
    • 好的,这很有道理。请注意,重定向和重写是 2 个真正不同的操作。如果您仍然有问题,请用您的规则和错误更新您的问题,我会看看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-25
    • 2014-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-28
    • 1970-01-01
    相关资源
    最近更新 更多