IIS7 URL 重写:如何不从重写的 URL 中删除 HTTPS 协议? [英] IIS7 URL Rewriting: How not to drop HTTPS protocol from rewritten URL?

查看:24
本文介绍了IIS7 URL 重写:如何不从重写的 URL 中删除 HTTPS 协议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个网站,该网站使用 IIS 7 的 URL 重写功能进行从 example.com 到 www.example.com 的永久重定向,以及从相似域名重写到主"域名,例如从 www.examples.com 到 www.example.com.

I'm working on a website that uses IIS 7's URL rewriting feature to do a permanent redirect from example.com to www.example.com, as well as rewrites from similar domain names to the "main" one, such as from www.examples.com to www.example.com.

这个重写规则 - 如下所示 - 已经运行了一段时间了.但是,我们最近添加了 HTTPS 支持,并注意到如果用户访问要重写为 www.example.com 的 URL 之一,则 HTTPS 将被删除.例如,如果用户访问 https://example.com,他们会被重定向到 http://www.example.com,而我们希望将它们发送到 https://www.example.com.

This rewrite rule - shown below - has worked well for some time now. However, we recently added HTTPS support and noticed that if users visit one of the URLs to be rewritten to www.example.com then HTTPS is dropped. For instance, if a user visits https://example.com they get redirected to http://www.example.com, whereas we would like them to be sent to https://www.example.com.

这是我们感兴趣的重写规则(在 Web.config 中):

Here is the rewrite rule of interest (in Web.config):

<rule name="Canonical Host Name" stopProcessing="true">
    <match url="(.*)" />

    <conditions logicalGrouping="MatchAny">
        <add input="{HTTP_HOST}" pattern="^example.com$" />
        <add input="{HTTP_HOST}" pattern="^(www.)?example.net$" />
        <add input="{HTTP_HOST}" pattern="^(www.)?example.info$" />
        <add input="{HTTP_HOST}" pattern="^(www.)?examples.com$" />
    </conditions>

    <action type="Redirect" url="http://www.example.com/{R:1}" redirectType="Permanent" />
</rule>

如你所见,action 元素的 url 属性直接指向 http://,所以我明白为什么 https://example.com 被重定向到 http://www.example.com.我的问题是,我该如何解决这个问题?我尝试(天真地)从 url 属性中删除 http://部分,但这没有用.

As you can see, the action element's url attribute points directly to http://, so I get why https://example.com is redirected to http://www.example.com. My question is, how do I fix this? I tried (naively) to just drop the http:// part from the url attribute, but that didn't work.

推荐答案

在同事的帮助下找到了答案.

Figured out the answer with some help from my colleagues.

我需要使用多个带有 {HTTPS} 条件的规则.请注意以下规则中的 {HTTPS} 条件.

I needed to use multiple rules with a condition on {HTTPS}. Note the {HTTPS} condition in the rules below.

<rule name="Canonical Host Name (HTTP)" stopProcessing="true">
    <match url="(.*)" />

    <conditions logicalGrouping="MatchAny">
        <add input="{HTTPS}" pattern="OFF" />
        <add input="{HTTP_HOST}" pattern="^example.com$" />
    </conditions>

    <action type="Redirect" url="http://www.example.com/{R:1}" redirectType="Permanent" />
</rule>

<rule name="Canonical Host Name (HTTPS)" stopProcessing="true">
    <match url="(.*)" />

    <conditions logicalGrouping="MatchAny">
        <add input="{HTTPS}" pattern="ON" />
        <add input="{HTTP_HOST}" pattern="^example.com$" />
    </conditions>

    <action type="Redirect" url="https://www.example.com/{R:1}" redirectType="Permanent" />
</rule>

然后我为备用域名重复了上面的规则对.

I then repeated the rule pair above for the alternate domain names.

这篇关于IIS7 URL 重写:如何不从重写的 URL 中删除 HTTPS 协议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆