页面应该从 http://example.com/重定向...https://www.example.com/...使用 301 重定向。
该页面应从 http://www.example.com/重定向...https://www.example.com/...使用 301 重定向。
该页面应从 https://example.com/重定向...https://www.example.com/...使用 301 重定向。
我在web.config中添加了以下URL重写。
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:0}" />
</rule>
<rule name="Redirect example.com to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="example.com" />
<add input="{HTTPS}" pattern="on" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" redirectType="Permanent"/>
</rule>
上面的代码无法正常工作。
- 当我们键入 https://example.com 时,它不会重定向到 https://www.example.com
请尝试这些规则,您也应该尝试重新启动应用程序池,以确保选取新规则。
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="true" />
</rule>
<rule name="Redirect non-www to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="example.com" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" />
</rule>
您的重定向应该可以正常工作,因为我刚刚尝试过它们。
尝试重置您网站的应用程序池或终止相应的 w3wp.exe 进程,因为模块 URLrewrite 正在缓存设置,如果您不强制重置所述缓存,有时可能需要相当长的时间才能使新的更改/规则正常工作。
还要确保主机 www.example.com 和 example.com 都使用 http,并且 https 都位于具有上述规则的同一 IIS 站点上。
要考虑的另一件事是浏览器缓存。浏览器缓存 301 重定向,这意味着如果您在某个时候设置不正确,您将在浏览器中缓存错误的重定向。
您需要清除浏览器中的重定向缓存才能解决此问题。
以下是从 Chrome 清除的步骤 https://superuser.com/questions/304589/how-can-i-make-chrome-stop-caching-redirects
Chrome菜单 Chrome 菜单> 设置> 显示高级设置...> 隐私> 单击清除浏览数据...
无论您选择什么其他内容,请确保"缓存的图像和文件"是 选中选项。
然后单击清除浏览数据,您应该可以再次重新测试。
如果您刚刚按照重定向操作,则只需删除数据 从过去一个小时开始。
或者,在隐身模式下进行测试和开发。缓存在那里 关闭浏览器后刷新。