不让别人知道你使用的是 WordPress 系统,提高 WordPress 站点安全性

文 / @UTHEME

关于给小本本的自我介绍

作为一个AI助手,我被赋予了语言处理和自然语言生成的能力,可以帮助用户完成各种文本相关的任务,例如文章改写、翻译、文本生成等。不过在这里,我更想和大家一起探讨技术相关的话题,例如网站开发、编程语言、算法等等。希望能和大家一起学习进步,共同成长。

URL重定向

为了保障网站的安全性,一些用户希望将WordPress后台隐藏起来。这里提供一种方法,可以帮助用户实现这个需求。首先,我们需要修改WordPress的目录结构,避免被其他人轻易地识别出来。具体而言,可以使用URL重定向的方式进行处理。以下是相关的代码实现:


/**
*URL重定向
*
*重定向:
*/wp-content/themes/themename/assets/css/到/assets/css/
*/wp-content/themes/themename/assets/js/到/assets/js/
*/wp-content/themes/themename/assets/img/到/assets/img/
*/wp-content/plugins/to/plugins/
*/
function nowp_add_rewrites($content){
global $wp_rewrite;
$nowp_new_non_wp_rules=array(
'assets/(.*)' => THEME_PATH.'/assets/$1',
'plugins/(.*)' => RELATIVE_PLUGIN_PATH.'/$1'
);
$wp_rewrite->non_wp_rules=array_merge($wp_rewrite->non_wp_rules,$nowp_new_non_wp_rules);
return $content;
}

function nowp_clean_urls($content){
if(strpos($content,RELATIVE_PLUGIN_PATH)>0){
return str_replace('/'.RELATIVE_PLUGIN_PATH,'/plugins',$content);
}else{
return str_replace('/'.THEME_PATH,'',$content);
}
}

//不重写多站点和自主体
if(!is_multisite()&&!is_child_theme()){
add_action('generate_rewrite_rules','nowp_add_rewrites');
if(!is_admin()){
$tags=array(
'plugins_url',
'bloginfo',
'stylesheet_directory_uri',
'template_directory_uri',
'script_loader_src',
'style_loader_src'
);
add_filters($tags,'nowp_clean_urls');
}
}

以上代码会将/wp-content/themes/themename/assets/文件夹重定向到/assets/,将/wp-content/plugins/重定向到/plugins/。如果您使用的是Apache服务器,WordPress会自动为您创建相关的.htacces文件。如果您使用的是Nginx服务器,则需要手动添加重写规则。

location ~ ^/assets/(img|js|css|fonts)/(.*)$ {
try_files $uri $uri/ /wp-content/themes/YOURTHEME/$1/$2;
}
location ~ ^/plugins/(.*)$ {
try_files $uri $uri/ /wp-content/plugins/$1;
}

需要注意的是,如果您在主题中修改了WP_CONTENT_URLWP_CONTENT_DIR常量,则以上代码中的wp-content目录需要相应地修改。

使用相对链接

WordPress中所有的链接一般都是使用绝对链接,但这并不是必需的。以下是将绝对链接修改为相对链接的代码:


/**
*修改绝对链接为相对链接
*
*提取自Roots主题
*/
function nowp_root_relative_url($input){
preg_match('|https?://([^/]+)(/.*)|i',$input,$matches);

if(isset($matches[1])&&isset($matches[2])&&$matches[1]===$_SERVER['SERVER_NAME']){
return wp_make_link_relative($input);
}else{
return $input;
}
}
function nowp_enable_root_relative_urls(){
return !(is_admin()||in_array($GLOBALS['pagenow'],array('wp-login.php','wp-register.php')));
}
$root_rel_filters=array(
'bloginfo_url',
'the_permalink',
'wp_list_pages',
'wp_list_categories',
'the_content_more_link',
'the_tags',
'get_pagenum_link',
'get_comment_link',
'month_link',
'day_link',
'year_link',
'tag_link',
'the_author_posts_link',
'script_loader_src',
'style_loader_src'
);
add_filters($root_rel_filters,'nowp_root_relative_url');

清理HTMLHead中没用的代码

WordPress在<head>中添加了许多我们平时用不到的代码,这会增加垃圾代码量,同时还会暴露网站后台系统的一些信息。以下代码可以清理这些多余的代码:


/**
*清理wp_head()
*
*移除不需要的<link>'s
*RemoveinlineCSSusedbyRecentCommentswidget
*RemoveinlineCSSusedbypostswithgalleries
*Removeself-closingtagandchange''sto"'sonrel_canonical()
*/
function nowp_head_cleanup(){
//Removejunkfromhead
remove_action('wp_head','rsd_link');
remove_action('wp_head','wp_generator');
remove_action('wp_head','feed_links',2);
remove_action('wp_head','index_rel_link');
remove_action('wp_head','wlwmanifest_link');
remove_action('wp_head','feed_links_extra',3);
remove_action('wp_head','start_post_rel_link',10,0);
remove_action('wp_head','parent_post_rel_link',10,0);
remove_action('wp_head','adjacent_posts_rel_link',10,0);
remove_action('wp_head','adjacent_posts_rel_link_wp_head',10,0);
remove_action('wp_head','wp_shortlink_wp_head',10,0);
remove_action('wp_head','feed_links',2);
remove_action('wp_head','feed_links_extra',3);

global $wp_widget_factory;
remove_action('wp_head',array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'],'recent_comments_style'));

if(!class_exists('WPSEO_Frontend')){
remove_action('wp_head','rel_canonical');
add_action('wp_head','nowp_rel_canonical');
}
}
function nowp_rel_canonical(){
global $wp_the_query;

if(!is_singular()){
return;
}

if(!$id=$wp_the_query->get_queried_object_id()){
return;
}

$link=get_permalink($id);
echo "\t<link rel=\"canonical\" href=\"$link\">\n";
}
add_action('init','nowp_head_cleanup');

/**
*RemovetheWordPressversion
*/
add_filter('the_generator','__return_false');

/**
*Cleanuplanguage_attributes()usedin<html>tag
*
*Changelang="en-US"tolang="en"
*Removedir="ltr"
*/
function nowp_language_attributes(){
$attributes=array();
$output='';

if(function_exists('is_rtl')){
if(is_rtl()=='rtl'){
$attributes[]='dir="rtl"';
}
}

$lang=get_bloginfo('language');

if($lang&&$lang!=='en-US'){
$attributes[]="lang=\"$lang\"";
}else{
$attributes[]='lang="en"';
}

$output=implode('',$attributes);
$output=apply_filters('nowp_language_attributes',$output);

return $output;
}
add_filter('language_attributes','nowp_language_attributes');

大功告成

经过以上的处理,网站的安全性会得到大大提升。虽然高手可能依然能够通过其他手段识别出网站所用的WordPress,但这个方法还是能够显著地降低风险,建议有一定动手能力的用户可以尝试使用。

添加UTHEME为好友
扫码添加UTHEME微信为好友
· 分享WordPress相关技术文章,主题上新与优惠动态早知道。
· 微信端最大WordPress社群,限时免费入群。