只重写子文件夹(HTTP到HTTPS)重定向到站点根目录
我想重写HTTP到我的IIS 8.5 Web服务器上特定子文件夹的HTTPS,但它不工作。我读过无数其他解决方案和博客文章,但没有我尝试过的作品。只重写子文件夹(HTTP到HTTPS)重定向到站点根目录
http://domain.example.com/one/two/three/
应该重定向到...(相同的URL,但使用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>
你应该避免这样做:
Within IIS I selected the desired subfolder (/three/ in the example above) and created the Rewrite rule there.
相反在应用程序根目录下的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
,那么您将不得不将上述内容合并到您的解决方案中。