【问题标题】:Rewrite subfolder only (HTTP to HTTPS) redirects to site root仅重写子文件夹(HTTP 到 HTTPS)重定向到站点根目录
【发布时间】:2017-01-04 02:35:29
【问题描述】:

我正在尝试将 IIS 8.5 Web 服务器上特定子文件夹的 HTTP 重写为 HTTPS,但它不起作用。我已经阅读了无数其他解决方案和博客文章,但我尝试过的都没有。

http://domain.example.com/one/two/three/
应该重定向到...(相同的网址,但使用 https)
https://domain.example.com/one/two/three/

而是重定向到...(使用 https 的站点根目录)
https://domain.example.com

正在加载...(带有 https 的所需网址)
https://domain.example.com/one/two/three/
还重定向到...(使用 https 的站点根目录)
https://domain.example.com

它正在从 url 中删除子文件夹。

此文件夹还需要使用 Windows 身份验证进行保护,我可以开始工作,但无论是否启用身份验证,https 重定向都会失败,所以我认为这不是原因。

在 IIS 中,我选择了所需的子文件夹(上例中的 /three/)并在那里创建了重写规则。

<rewrite>
    <rules>
        <clear />
        <rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true">
            <match url="(.*)" />
            <conditions logicalGrouping="MatchAny">
                <add input="{HTTPS}" pattern="^OFF$" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="SeeOther" />
        </rule>
    </rules>
</rewrite>

这当然适用于所需子文件夹中包含的任何文件和文件夹。 (/三)

我试过这个,它重定向到明显正确的 url,但给出“重定向太多”错误:

<rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAll">
        <add input="{HTTPS}" pattern="^OFF$" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="SeeOther" />
</rule>

【问题讨论】:

    标签: redirect iis-8 url-rewrite-module


    【解决方案1】:

    你应该避免这样做:

    在 IIS 中,我选择了所需的子文件夹(示例中为 /three/ 上面)并在那里创建了重写规则。

    改为在应用程序根目录的Web.config 中设置重写规则。您可以通过将特定文件夹包含在 match 参数中来将其重定向到 HTTPS,如下所示:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="Redirect Subfolder" stopProcessing="true">
                        <match url="^one/two/three/" />
                        <conditions logicalGrouping="MatchAll">
                            <add input="{HTTPS}" pattern="^OFF$" />
                        </conditions>
                        <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" />
                    </rule>
                </rules>
            </rewrite>
        </system.webServer>
    </configuration>
    

    请注意,这是一个最小的Web.config 文件,可以满足您的需求。如果您的应用程序的根文件夹中已经包含 Web.config,那么您必须将上述内容合并到您的解决方案中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-08
      • 2011-01-14
      • 2012-10-15
      • 2017-06-01
      • 1970-01-01
      • 2021-11-24
      • 2012-10-23
      • 2017-01-29
      相关资源
      最近更新 更多