技术交流

当前位置:首页 >> 技术交流 >> IIS中不带www的顶级域名301重定向的方法

IIS中不带www的顶级域名301重定向的方法

发布:2022-08-23

1、在云主机或服务器上安装URL Rewrite组件,微软官网下载地址:https://www.iis.net/downloads/microsoft/url-rewrite

URL Rewrite组件介绍:IIS URL Rewrite 2.1使Web管理员能够创建强大的规则,以实现更容易让用户记住和更容易让搜索引擎收录、更加友好并符合搜索引擎优化规则的URL。通过使用规则模板以及集成在IIS管理器中的其他功能,Web管理员可以轻松地设置规则,根据HTTP头、HTTP响应或请求头、IIS服务器变量,甚至是复杂的编程规则来定义URL重写行为。此外,管理员可以根据重写规则中表达的逻辑,执行重定向,发送自定义响应,或停止HTTP请求。

2、在网站web.config文件中增加代码,注意,添加位置在:

<system.webServer></system.webServer>
之间:

<rewrite>
    <rules>
        <clear />
        <rule name="IIS 301 Redirect non-www to www" stopProcessing="true">
            <match url=".*" />
            <conditions>
                <add input="{CACHE_URL}" pattern="^(.+)://(?!www)(.*)" />
            </conditions>
            <action type="Redirect" url="{C:1}://www.{C:2}" redirectType="Permanent" />
        </rule>
        <rule name="Redirect HTTP to HTTPS" enabled="true" stopProcessing="true">
            <match url="(.*)" />
            <conditions logicalGrouping="MatchAny">
                <add input="{SERVER_PORT_SECURE}" pattern="^0$" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
        </rule>
    </rules>
</rewrite>
声明:以上文章素材和专业知识由本站人工原创整理,谢绝转载。
标签: IIS www 301 重定向