让蜘蛛爬掉https的加密站点

因为强制应用https证书,搜索引擎根本不能爬(除google),没有了搜索流量,所以基本上读者都是固定读者。

如上图所示,基本上都是从google过来的访问者。yahoo也只有一位而已。所以我再想:能不能在强制应用https情况下让搜索引擎收录我的小站呢?

答案是肯定的(废话~)。因为除了google外,其他的搜索引擎只能爬http,所以根据搜索引擎蜘蛛的user-agent判断并redirect到http即可。(话说改php,写htaccess好辛苦……)

什么是user-agent

就是用来判别访问者身份,操作系统,浏览器等信息的一个标识字符串。通过这个标识,用户所访问的网站可以显示不同的排版、内容等,从而为用户提供更好的体验或者进行信息统计等。

为了测试修改,您需要使用firefox浏览器,并安装 user-agent switcher 插件。

一些搜索引擎的user-agent:
  • google: Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)
  • msnbot: msnbot/1.1 (+http://search.msn.com/msnbot.htm)
  • yahoo  slurp : Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)
  • 其他搜索引擎您可以在user-agent串中加入相应关键字以构建相应user-agent
  • 修改 htaccess 文件

首先您需要确认您已经开启apache的rewrite模块。

4
5
6
7
8
9
10
RewriteEngine On
  
RewriteCond %{HTTPS} !on [NC]                                 //https
RewriteCond %{HTTP_USER_AGENT} !(baiduspider|googlebot|soso|bing|sogou|yahoo|sohu-search|yodao|robozilla|msnbot) [NC]
//如果user-agent中包含如上字符,则不重定向到https
RewriteRule (.*) https: //%{SERVER_NAME}%{REQUEST_URI} [R=301,NC,L]
//根据规则,重定向至https,即http访问时,强制使用https加密。

加入搜索引擎的user-agent字符,在重定向时候判断是否为搜索引擎,如果是搜索引擎则不重定向至https,仍然让搜索引擎的蜘蛛以http方式访问;如果不是搜索引擎则重定向至https加密模式(即普通用户使用浏览器浏览时使用https加密)。

验证效果:使用firefox浏览器,通过user-agent switcher插件更改浏览器user-agent为搜索引擎的user-agent。以http方式访问,可以看出并不会跳转至https方式;以浏览器默认user-agent方式访问,则自动重定向至https加密模式。

  • 修改wordpress

如果您像我一样对wordpress强制https,那么肯定也是使用了force_ssl插件并修改了wp-settings.php文件。所以我们需要修改这两个文件以实现相应的功能。

首先修改/var/www/blog/wp-content/plugins/force_ssl/force-ssl.php文件。

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
  
function force_ssl() {
     if ( $_SERVER [ "HTTPS" ] != "on" )
  {    if ( $_SERVER [ "REQUEST_URI" ] != "/blog/feed/" )
//feed不可以使用https,否则feedburner等烧录工具和阅读器可能无法正常抓取
     if (get_naps_bots() ==false) {
     //判断user-agent,如果不是搜索引擎,则构建https路径并redirect
         $newurl = "https://" . $_SERVER [ "SERVER_NAME" ] . $_SERVER [ "REQUEST_URI" ];
         //构建https路径
         wp_redirect( $newurl );
         //redirect
  
         exit ();
     }
//这里无需对搜索引擎进行处理,因为搜索引擎是以http方式访问,只要不对其进行redirect即可。
  
     }
     }
     }
  
function get_naps_bots()
//判断user-agent是否为搜索引擎并返回值
{
$useragent = strtolower ( $_SERVER [ 'HTTP_USER_AGENT' ]);
if ( strpos ( $useragent , 'baiduspider' ) !== false){
return 'baiduspider' ;
}
if ( strpos ( $useragent , 'googlebot' ) !== false){
return 'googlebot' ;
}
if ( strpos ( $useragent , 'soso' ) !== false){
return 'soso' ;
}
if ( strpos ( $useragent , 'bing' ) !== false){
return 'bing' ;
}
if ( strpos ( $useragent , 'yahoo' ) !== false){
return 'yahoo' ;
}
if ( strpos ( $useragent , 'sohu-search' ) !== false){
return 'Sohubot' ;
}
if ( strpos ( $useragent , 'yodao' ) !== false){
return 'yodao' ;
}
if ( strpos ( $useragent , 'robozilla' ) !== false){
return 'Robozilla' ;
}
if ( strpos ( $useragent , 'msnbot' ) !== false){
return 'msnbot' ;
}
return false;
}
add_action( 'plugins_loaded' , 'force_ssl' );
  
?>

修改/var/www/blog/wp-settings.php文件:

在大约390行处找到如下内容,并注释掉(如果您以前修改过,可以跳过此步骤)

4
5
if ( !defined( 'WP_CONTENT_URL' ) )
define( 'WP_CONTENT_URL' , get_option( 'siteurl' ) . '/wp-content' ); // full url - WP_CONTENT_DIR is defined further up

在其后添加如下代码:

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Fix the URL root for SSL
function fix_ssl_siteurl( $url ) {
     $scheme = (is_ssl() ? 'https' : 'http' );
     if (0 === strpos ( $url , 'http' )) {
         if (is_ssl())
             //search engine no direct
             if ( get_naps_bot !== false){
             $url = str_replace ( 'http://' , "{$scheme}://" , $url ); }
     }
  
     return $url ;
}
add_filter( 'option_siteurl' , fix_ssl_siteurl);
add_filter( 'option_home' , fix_ssl_siteurl);
  
if ( !defined( 'WP_CONTENT_URL' ) )
     define( 'WP_CONTENT_URL' , get_option( 'siteurl' ) . '/wp-content' ); // full url - WP_CONTENT_DIR is defined further up

然后再加入这个函数:

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//identify search engint
function get_naps_bot()
{
$useragent = strtolower ( $_SERVER [ 'HTTP_USER_AGENT' ]);
  
if ( strpos ( $useragent , 'baiduspider' ) !== false){
return 'baiduspider' ;
}
  
if ( strpos ( $useragent , 'googlebot' ) !== false){
return 'googlebot' ;
}
  
if ( strpos ( $useragent , 'soso' ) !== false){
return 'soso' ;
}
  
if ( strpos ( $useragent , 'bing' ) !== false){
return 'bing' ;
}
  
if ( strpos ( $useragent , 'yahoo' ) !== false){
return 'yahoo' ;
}
  
if ( strpos ( $useragent , 'sohu-search' ) !== false){
return 'Sohubot' ;
}
  
if ( strpos ( $useragent , 'yodao' ) !== false){
return 'yodao' ;
}
  
if ( strpos ( $useragent , 'robozilla' ) !== false){
return 'Robozilla' ;
}
  
if ( strpos ( $useragent , 'msnbot' ) !== false){
return 'msnbot' ;
}
return false;
}
$issearchbot = get_naps_bot();

保存上边修改过的两个php文件后,就可以在wordpress实现:对搜索引擎使用http,对其他用户使用https加密方式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值