接着上一篇文章 《WordPress 网站终极防黑手册,全方位保护网站安全 (一) 》小编继续来谈谈 WordPress 系统的一些已知安全隐患。老鸟级别的 WordPress 站长一定知道不管在任何版本的 WordPress 程序上作者存档页面的固定链接都是不能修改的,而且一旦站长开启伪静态后,只要直接访问
abc.com/?author=$id (PS:id 为用户的数字 id)
则 WordPress 会直接转跳到
abc.com/author/用户帐号/
这种形式的链接,这将直接暴露出网站用户的登录帐号,存在着一定的安全隐患。 (PS:管理员的 ID 都是 1-5 这五个数字,直接一试管理员帐号就暴露了,然后密码字典你懂的

。)
查看 WordPress 源文件发现作者存档页面链接是这样获取的:
/**
* Retrieve the URL to the author page for the user with the ID provided.
*
* @since 2.1.0
* @uses $wp_rewrite WP_Rewrite
* @return string The URL to the author's page.
*/
function get_author_posts_url($author_id, $author_nicename = '') {
global $wp_rewrite;
$auth_ID = (int) $author_id;
$link = $wp_rewrite->get_author_permastruct();
if ( empty($link) ) {
$file = home_url( '/' );
$link = $file . '?author=' . $auth_ID;
} else {
if ( '' == $author_nicename ) {
$user = get_userdata($author_id);
if ( !empty($user->user_nicename) )
$author_nicename = $user->user_nicename;
}
$link = str_replace('%author%', $author_nicename, $link);
$link = home_url( user_trailingslashit( $link ) );
}
$link = apply_filters('author_link', $link, $author_id, $author_nicename);
return $link;
}
那么只要重写下规则即可改变作者存档页面的链接,这里以作者 ID 为例:(将以下代码加入到 functions.php 文件)
add_filter( 'request', 'wxd_author_link_request' );
function wxd_author_link_request( $query_vars ) {
if ( array_key_exists( 'author_name', $query_vars ) ) {
global $wpdb;
$author_id=$query_vars['author_name'];
if ( $author_id ) {
$query_vars['author'] = $author_id;
unset( $query_vars['author_name'] );
}
}
return $query_vars;
}
add_filter( 'author_link', 'wxd_author_link', 10, 2 );
function wxd_author_link( $link, $author_id) {
global $wp_rewrite;
$author_id = (int) $author_id;
$link = $wp_rewrite->get_author_permastruct();
if ( empty($link) ) {
$file = home_url( '/' );
$link = $file . '?author=' . $author_id;
} else {
$link = str_replace('%author%', $author_id, $link);
$link = home_url( user_trailingslashit( $link ) );
}
return $link;
}
再次访问
abc.com/?author=$id
则转跳到
abc.com/author/$id
这样一来就不会暴露网站用户以及管理员账号了。