繁体   English   中英

如何将所有非www URL重定向到https:// www。 在IIS?

[英]How to redirect all non-www URLs to https://www. in IIS?

我想在IIS 8.5中添加正确的301永久重定向规则。 我添加了以下规则,但它不起作用。

 <rule name="Redirect top domains with non-www to www" stopProcessing="true">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
            <add input="{HTTP_HOST}" pattern=".*stage\..*" negate="true" />
            <add input="{HTTP_HOST}" pattern=".*dev\..*" negate="true" />
            <add input="{HTTP_HOST}" pattern="^(http:\/\/){0,1}(www\.){0,1}([^\.]+)\.([^\.]+)$" ignoreCase="true" />
          </conditions>
          <action type="Redirect" url="https://www.{C:3}.{C:4}" redirectType="Permanent" />
        </rule>

条件

总而言之, 每个URL都应该是HTTPS,并且应该有“www”。 前缀

注意:我在IIS中安装了URL重写模块

任何人都可以帮助我实现这一目标吗?

我通过在Web.config文件中添加两个URL重写规则来管理它:

  1. 用于将非www重定向到https:// www 。{domain} .com / ...
  2. 将HTTP重定向到HTTPS

     <rule name="Redirect top domains with non-www to www" stopProcessing="true"> <match url="(.*)" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> <add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" /> <add input="{HTTP_HOST}" pattern=".*stage\\..*" negate="true" /> <add input="{HTTP_HOST}" pattern=".*dev\\..*" negate="true" /> <add input="{HTTP_HOST}" pattern="^([^\\.]+)\\.([^\\.]+)$" /> </conditions> <action type="Redirect" url="https://www.{HTTP_HOST}/{R:1}" redirectType="Permanent" /> <serverVariables> <set name="Redirect" value="false" /> </serverVariables> </rule> <rule name="Force HTTPS" enabled="true" stopProcessing="true"> <match url="(.*)" ignoreCase="false" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> <add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" /> <add input="{HTTP_HOST}" pattern=".*stage\\..*" negate="true" /> <add input="{HTTP_HOST}" pattern=".*dev\\..*" negate="true" /> <add input="{HTTPS}" pattern="off" /> </conditions> <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" /> </rule> 

所有使用negate="true"的条件都用于排除。 因此,包含“localhost”,“stage”和“dev”的所有URL都将从URL重写中排除。 如果不需要,您可以删除这些条件。

有关negate属性的更多信息, 请访问http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM