【问题标题】:IIS configuration for URL rewrite throwing Http404 error用于 URL 重写的 IIS 配置引发 Http404 错误
【发布时间】:2019-12-28 08:41:36
【问题描述】:

我正在开发一个需要将 URL 从 http 重定向到 https 的网站。我的 URL 看起来像:(我每次都使用随机令牌 ID 生成此 URL)

http://testsite.local/login.aspx/activate?token=(random生成的令牌id)

我想把这个网址重定向到

https://testsite.local/login.aspx/activate?token=(random生成的令牌id)

下面是 IIS 中的 URL 重写设置:

Web.config 文件:

 <rewrite>
            <rules>
                <rule name="http redirect">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="^OFF$" />
                    </conditions>
                    <action type="Rewrite" url="https://{HTTP_HOST}{REQUEST_URI}" logRewrittenUrl="true" />
                </rule>
            </rules>
        </rewrite>

当我输入 URL 时,我会看到 Http404 错误。在这里提一下 - 我检查了 IIS 中的“需要 SSL”条件。我知道匹配模式看起来错误,但不确定修复。有什么帮助吗?

【问题讨论】:

    标签: asp.net http iis https iis-7


    【解决方案1】:

    尝试将您的 {REQUEST_URI} 更改为 {R:1} 可能会解决此问题。

    • {R:x} 用作来自规则模式 () 的反向引用。

    在我的机器上工作。

    像这样:

    <rule name="HTTPS rewrite" enabled="true" stopProcessing="true"> 
              <match url="(.*)"/>  
              <conditions> 
                <add input="{HTTPS}" pattern="^OFF$"/> 
              </conditions>  
              <action type="Rewrite" url="https://{HTTP_HOST}{R:1}" logRewrittenUrl="true" />
            </rule> 
    

    顺便说一句,您提到您需要将Redirect HTTP 流量转为 HTTPS。但您似乎正在尝试将Rewrite HTTP 流量发送到用作反向代理的 HTTPS。

    如果您想配置正确的 301 HTTP 重定向,请使用 Redirect 而不是 Rewrite

    像这样:

            <rule name="HTTPS force" enabled="true" stopProcessing="true"> 
              <match url="(.*)"/>  
              <conditions> 
                <add input="{HTTPS}" pattern="^OFF$"/> 
              </conditions>  
              <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent"/> 
            </rule> 
    

    【讨论】:

      【解决方案2】:

      我遵循了@Anduin 提到的更改。除了这些更改之外,我还为该站点创建了一个端口 80 绑定,并且该规则运行良好。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-01
        • 2011-03-01
        • 2017-11-24
        • 1970-01-01
        • 2016-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多