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

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

问题描述

我正在使用IIS 7的网址重写功能的网站上进行从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天全站免登陆