我的博客是强制使用https加密链接的,为什么呢?因为我不想自己写完东西还自我审察一边,比如把“敏感词”写成“敏|感|词”。

但是这样也带来了一些问题,除google以外的搜索引擎全都不收录了。然后找到了一个挺不错的方法,把蜘蛛的user-agent重定向到http <link>

先修改.htaccess文件

判断UA并将搜索引擎定向到http,将一般访客定向到https

1
2
3
4
5
6
7
8
9
RewriteEngine On

# BEGIN Force SSL

RewriteCond %{HTTPS} !on [NC]
RewriteCond %{HTTP_USER_AGENT} !(baiduspider|googlebot|soso|bing|sogou|yahoo|sohu-search|yodao|robozilla|msnbot) [NC]
RewriteRule (.*) https://%{SERVER_NAME}%{REQUEST_URI} [R=301,NC,L]

# END Force SSL

添加force-ssl插件

1
/* Plugin Name: Force SSL Plugin URI: http://www.almosteffortless.com/wordpress/force-ssl/ Description:  For those will an SSL certificate, this plugin forces an HTTPS connection for security purposes. Version: 1.0 Author: Trevor Turk */ function force_ssl() { if($_SERVER["HTTPS"] != "on") { if ($_SERVER["REQUEST_URI"] != "/blog/feed/") { if (get_naps_bots() ==false) { $newurl = "https://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]; wp_redirect($newurl); exit(); } } } } function get_naps_bots() { $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');

保存 force-ssl.php 上传到/wp-content/plugins/force-ssl

修改wp-settings.php

添加:

1
2
3
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
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
//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();

这样就好了,自己改了GoogleBot的UA试了下效果,成功了。

Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)

等过两天再看看搜索引擎收录情况吧 :-P