【问题标题】:assistance needed with iis rewrite rule in web.configweb.config 中的 iis 重写规则需要帮助
【发布时间】:2013-02-01 18:08:17
【问题描述】:

我正在使用在 IIS 7.5 中运行的 mvc4 应用程序,但我在设置的重写规则方面遇到了问题。以下是我在 web.config 中的部分。

 <rewrite>
        <rules>
            <rule name="RewriteImage">
                <match url="/myassets/([_0-9a-z-]+)/images/category/([_0-9]+)-([_0-9a-z-]+)-([0-9]+)" />
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                </conditions>
                <action type="Rewrite" url="/myassets/{R:1}/images/category/cat_{R:2}_{R:4}_{R:3}.jpg" />
            </rule>
        </rules>
    </rewrite>

我想在上面的 url 中将图像重写到另一个路径。

我提供以下网址 https://localhost/myassets/en-uk/images/category/123456-5x10-1_my+image+description.jpg 并希望它重写为 https://localhost/myassets/en-uk/images/category/cat_123456_1_5x10.jpg 但它没有。

当我在 IIS 中对其进行测试时,一切正常,但通过浏览器,规则永远不会生效。我正在寻找有关我哪里出错的建议,因为这变得非常令人沮丧!

这是我唯一的规则,我尝试了一个简单的重定向规则,它将大写 URL 重定向到小写 URL,这很好。

我在 web.config 中注意到的一件事是,第一个 rewrite 标记在下面有一个蓝色波浪线,表示它是 system.webserver 的未识别部分。

应用程序正在集成模式下运行。

【问题讨论】:

  • 你怎么知道规则永远不会生效?文件https://localhost/myassets/en-uk/images/category/cat_123456_1_5x10.jpg 存在吗?
  • 如果你删除&lt;add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /&gt;会怎样?
  • 如果我这样做也没关系
  • 所以图片https://localhost/myassets/en-uk/images/category/cat_123456_1_5x10.jpg 存在,当您调用https://localhost/myassets/en-uk/images/category/123456-5x10-1_my+image+description.jpg 时它不会显示它,对吧?反而看到了什么?如果您使用Failed Request Tracing tool 会怎样?

标签: iis asp.net-mvc-4 url-rewriting iis-7.5


【解决方案1】:

问题是您的正则表达式无效。来自official documentation的备注:

例如,如果对该 URL 发出请求: http://www.example.com/content/default.aspx?tabid=2&subtabid=3 和一个 然后在站点级别定义了重写规则:规则模式获取 URL 字符串 content/default.aspx 作为输入。

这意味着,你不应该用斜杠开始你的正则表达式。

工作示例是:

<rule name="RewriteImage">
    <match url="myassets/([_0-9a-z-]+)/images/category/([_0-9]+)-([_0-9a-z-]+)-([0-9]+)" />
    <action type="Rewrite" url="/myassets/{R:1}/images/category/cat_{R:2}_{R:4}_{R:3}.jpg" />
</rule> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多