云服务器网:购买云服务器和VPS必上的网站!

IIS 8.5的301重定向代码,www重定向到没有www域名

windows server 2012中的IIS8.5,如何实现将带www的顶级域名,301重定向到不带www…

windows server 2012中的IIS8.5,如何实现将带www的顶级域名,301重定向到不带www域名中(就是二级域名301到顶级域名)。要想实现www的顶级域名做301重定向到不带www的域名中,就需要使用IIS的URL重写模块,而不是使用重定向功能。

以下文件直接在web.config配置文件中修改:

以下是IIS 8.5的301重定向代码示例:

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Redirect to HTTPS" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

这个示例代码可以将HTTP请求自动重定向到HTTPS。将此代码添加到您的IIS Web.config文件中即可生效。

以下是将www域名重定向到没有www的域名的IIS 8.5示例重定向代码:

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Redirect to non-WWW" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^www\.(.*)$" />
          </conditions>
          <action type="Redirect" url="http://{C:1}/{R:0}" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

将此代码添加到您的IIS Web.config文件中即可生效,并将www域名重定向到没有www的域名。请确保在执行此操作之前备份您的现有Web.config文件。

本文来源:https://yuntue.com/post/46462.html | 云服务器网,转载请注明出处!

关于作者: yuntue

云服务器(www.yuntue.com)是一家专门做阿里云服务器代金券、腾讯云服务器优惠券的网站,这里你可以找到阿里云服务器腾讯云服务器等国内主流云服务器优惠价格,以及海外云服务器、vps主机等优惠信息,我们会为你提供性价比最高的云服务器和域名、数据库、CDN、免费邮箱等企业常用互联网资源。

为您推荐

发表评论

您的电子邮箱地址不会被公开。