网站启用https后根据搜索引擎进行跳转的.htaccess代码

全站启用https后,部分搜索引擎如360搜索和神马搜索等并不支持https,那么怎么办呢?经过本人的摸索,可以通过.htaccess设置跳转,如果网站不支持,也可通过PHP进行301跳转。

.htaccess根据搜索引擎进行网址跳转跳转代码

#360、神马和XP系统不跳转,By https://weicho.net
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{HTTP_USER_AGENT} !(Chrome|360Spider|YisouSpider|Windows\ NT\ 5.1|Windows\ NT\ 5.2) [NC] 
RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [L,R=301]
</IfModule>

将以上代码放在根目录.htaccess中即可,注意放置的位置。

php根据搜索引擎进行网址跳转跳转代码

//Baiduspider等直接跳转到https,By https://wecho.net
if(!((isset($_SERVER['HTTPS'])&&$_SERVER['HTTPS']=='on')||(isset($_SERVER['HTTP_X_FORWARDED_PROTO'])&&$_SERVER['HTTP_X_FORWARDED_PROTO']=='https'))){
    $userAgent = $_SERVER['HTTP_USER_AGENT'];
    if(stripos($userAgent,"Baiduspider") || stripos($userAgent,"bingbot") || stripos($userAgent,"Googlebot") || stripos($userAgent,"Sogou web spider") || stripos($userAgent,"YandexBot") || stripos($userAgent,"Chrome") || stripos($userAgent,"Safari") || stripos($userAgent,"Firefox") || stripos($userAgent,"360SE") || stripos($userAgent,"Mozilla") || stripos($userAgent,"UCBrowser") || stripos($userAgent,"MSIE") || stripos($userAgent,"Trident") || stripos($userAgent,"Edge") || stripos($userAgent,"MicroMessenger") || stripos($userAgent,"UCWEB") || stripos($userAgent,"MQQBrowser") || stripos($userAgent,"QQ") || stripos($userAgent,"AppleWebKit")){
    Header("HTTP/1.1 301 Moved Permanently");
    header('Location: https://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
    }
}

将以上代码防止在网站根目录index.php即可,注意放置的位置。

猜您喜欢

发表评论

 欢迎您发表评论!