IIS URL 使用端口将 HTTP 重写为 HTTPS

And*_*ndi 12 rewrite iis-7 url https redirect

我的网站有两个绑定:1000 和 1443 (端口 80/443 被同一 IIS 实例上的另一个网站使用)端口 1000 是 HTTP端口 1443 是 HTTPS。我想要做的是使用“htt p://server:1000”将任何传入请求重定向到"https://server:1443". 我正在玩 IIS 7 重写模块 2.0,但我正在用头撞墙。任何见解表示赞赏!

顺便说一句,下面的重写配置适用于在端口 80 上具有 HTTP 绑定和在端口 443 上具有 HTTPS 绑定的站点,但它不适用于我的端口。

PS 我的 URL 故意留有空格,因为“垃圾邮件预防机制”启动了。出于某种原因,谷歌登录不再工作,所以我不得不创建一个 OpenID 帐户(没有脚本可能是罪魁祸首)。我不确定如何让 XML 显示得很好,所以我在左括号后添加了空格。

< ?xml version="1.0" encoding="utf-8"?>
< configuration>
  < system.webServer>
    < rewrite>
      < rules>
        < rule name="HTTP to HTTPS redirect" stopProcessing="true">
          < match url="(.*)" />
          < conditions trackAllCaptures="true">
                        < add input="{HTTPS}" pattern="off" />
          < /conditions>
          < action type="Redirect" redirectType="Found" url="htt ps: // {HTTP_HOST}/{R:1}" />
        < /rule>
      < /rules>
    < /rewrite>
  < /system.webServer>
< /configuration>
Run Code Online (Sandbox Code Playgroud)

小智 12

虽然这个问题已经回答了一段时间,但这是我使用的规则。

<rule name="Redirect to HTTP on different port" enabled="true" stopProcessing="true">
      <match url="(.*)"/>
      <conditions>
        <add input="{HTTPS}" pattern="ON"/>
      </conditions>
      <!-- Cannot Use {HTTP_HOST} as it contains both the {SERVER_NAME}{SERVER_PORT} -->
      <action type="Redirect" url="http://{SERVER_NAME}:1000{HTTP_URL}" redirectType="Found" appendQueryString="false"/>
    </rule>
Run Code Online (Sandbox Code Playgroud)


And*_*ndi 8

            <rule name="HTTP to HTTPS on different SSL Port" enabled="true" stopProcessing="true">
                <match url="(.*)" ignoreCase="true" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
                    <add input="{HTTPS}" pattern="off" />
                    <add input="{HTTP_HOST}" pattern="([^/:]*?):[^/]*?" />
                </conditions>
                <action type="Redirect" url="https://{C:1}:1443/{R:0}" appendQueryString="false" />
            </rule>
Run Code Online (Sandbox Code Playgroud)

这解决了我的问题。

  • 它可能应该是:([^/:]+)(:[^/]*)?因为端口可能并不总是在那里。 (2认同)
  • 对于任何未来的 googlers:看起来这个答案来自 [this thread](http://forums.iis.net/t/1174819.aspx?Need+to+redirect+HTTP+to+HTTPS+on+non+standard +ports)在 IIS 论坛上。那里给出了正则表达式的完整描述。 (2认同)

Dsc*_*duc 1

尝试改变这个:

<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}"/>
Run Code Online (Sandbox Code Playgroud)

对此:

<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}:1443/{R:1}"/>
Run Code Online (Sandbox Code Playgroud)