万企互联-专注高端网站建设
扫描关注万企互联微信公众账号

扫一扫微信二维码

全站301跳转不带www域名,排除本地和二级域名

编程代码2022/2/20 304

带www与不带www的域名,在域名解析和网站管理上都可以是独立的,但是除非特别的原因,否则绝大多数网站带www与不带www的域名都是指向同一个站点,网站内容是不分开的,然而尽管如此,搜索引擎仍然会认为带www与不带www的域名是代表两个不同的网站,在收录和权重分配上仍然是分开的。这就是所谓的“权重分散”,对于网站SEO优化来说,是一个需要特别重视的部分。本文将要介绍几种方法,避免带www与不带www域名的权重分散。

IIS重定向(web.config)配置:

方法1:
        <rewrite>
            <rules>
                <rule name="Non-WWW only" stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^(.*)wan-qi.cn$" ignoreCase="false" negate="true" />
                        <add input="{HTTP_HOST}" pattern="^localhost$" ignoreCase="false" negate="true" />
                        <add input="{HTTP_HOST}" pattern="^www\." ignoreCase="false" negate="true" />
                    </conditions>
                    <action type="Redirect" redirectType="Permanent" url="http://www.{HTTP_HOST}/{R:1}" />
                </rule>
            </rules>
        </rewrite>
方法2:
        <rewrite>
            <rules>
                <rule name="Host Name" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^(.*)wan-qi.cn$" />
                    </conditions>
                    <action type="None" />
                </rule>
                <rule name="Non-WWW only" stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^www." ignoreCase="false" negate="true" />
                    </conditions>
                    <action type="Redirect" redirectType="Permanent" url="http://www.{HTTP_HOST}/{R:1}" />
                </rule>
            </rules>
        </rewrite>

ASP代码:

<%
Servername=CStr(Request.ServerVariables("Server_Name"))
if Servername="localhost" or Instr(Servername,"wqhl.cn")>0 then
else
    strurl=Replace(weburl,"http://","")
    strurl=Replace(strurl,"/","")
    If strurl<>Servername Then
        ScriptAddress=CStr(Request.ServerVariables("SCRIPT_NAME"))
        qs=Request.QueryString
        if qs<>"" then
            Url ="http://"&strurl&ScriptAddress &"?"&qs
        else
            Url ="http://"&strurl&ScriptAddress
        end if
        Response.Status="301 Moved Permanently" 
        Response.AddHeader "Location", Url 
        Response.End 
    end If
end If
%>

PHP代码:

<?php   
      $the_host = $_SERVER['HTTP_HOST'];//取得当前域名   
      $request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';//判断地址后面是否有参数   
      if($the_host == 'webkaka.com')//把这里的域名换上你想要的   
      {   
            header('HTTP/1.1 301 Moved Permanently');//发出301头部   
            header('Location: http://www.wan-qi.cn'.$request_uri);//跳转到你希望的地址格式   
      }  
?>
文章关键词
301
跳转
域名
品控技术网 TOP图标库 万企互联 咸阳网站建设 域名转发系统 IP地址查询 万企工具 超越彼岸BEYOND 六佰号 秦川云 IDC主机测评 三秦人才网 陕西锦新橡塑制品有限公司