当前位置:首页>>技术文章>>内容详情

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

2022年02月20日
Tags:301跳转域名

带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="^(.*)pindoo.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="^(.*)pindoo.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,"pindoo.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.webkaka.com'.$request_uri);//跳转到你希望的地址格式   
      }  
?>
打开微信,点击底部的“发现”,使用“扫一扫”即可将网页分享至朋友圈。
上一条信息:会员页面用二级域名实现展示
下一条信息:使用 JavaScript 生成二维码 QRCode.js