前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >隐藏 WordPress 系统,提高 WordPress 站点安全性

隐藏 WordPress 系统,提高 WordPress 站点安全性

作者头像
许都博客
修改2021-06-27 15:14:55
1.3K0
修改2021-06-27 15:14:55
举报
文章被收录于专栏:许都博客许都博客

出于保密和安全方面的原因,一些用户会考虑把网站所用的后台隐藏起来,这中需求有一个收费插件叫「hide my wp」,如果你没有办法购买,或者不想使用插件,这篇文章就是为你准备的。本文中的方法适合有一定动手能力的用户使用。下面我们就来看看怎么一步一步地把 WordPress 隐藏掉。

URL 重定向

WordPress 的目录结构是最具特色的,如果不经过处理,别人通过代码一眼就能看出来你的网站是用的 WordPress,我们首先需要做的就是修改掉 WordPress 的目录结构。

代码语言:javascript
复制
/**
 * 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(
    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');
    }
}

以上代码假设在你的主题中有/assets/文件夹,如果你使用的是 Apache 服务器,WordPress 会自动为你重建好重写需要的.htacces文件,如果你使用的是 Nginx,还需要手动添加重写规则到你的主机配置文件中。

代码语言:javascript
复制
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/
}

上面的规则硬编码了 /wp-content/ 目录,如果你在主题中修改了 WP_CONTENT_URL 或 WP_CONTENT_DIR 常量,可能会出现冲突,确保以上代码中的 wp-content 目录是正确的就可以了。

使用相对链接

所有的地方都使用绝对链接也是 WordPress 的一大特点,其实这是没有必要的,我们通过下面的代码可以吧绝对链接修改成为相对链接。

代码语言:javascript
复制
/**
 * 修改绝对链接为相对链接
 *
 * 提取自Roots主题
 */
function nowp_root_relative_url($input) {
    preg_match('|https?://([^/]+)(/.*)|i', input, matches);
 
    if (isset(matches[1]) && isset(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');

清理 HTML Head 中没用的代码

WordPress 在 <head> 中添加了很多我们平时用不到的代码,这不但增加了垃圾代码,对网站后台系统也暴露得很充分,好在我们可以很容易的清理掉这些代码,添加以上代码到主题的 functions.php 文件中即可。

代码语言:javascript
复制
/**
 * 清理wp_head()
 *
 * 移除不需要的 <link>'s
 * Remove inline CSS used by Recent Comments widget
 * Remove inline CSS used by posts with galleries
 * Remove self-closing tag and change ''s to "'s on rel_canonical()
 */
function nowp_head_cleanup() {
    // Remove junk from head
    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');
 
/**
 * Remove the WordPress version
 */
add_filter('the_generator', '__return_false');
 
/**
 * Clean up language_attributes() used in <html> tag
 *
 * Change lang="en-US" to lang="en"
 * Remove dir="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 && 
 attributes[] = "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 信息,大大提高 WordPress 的安全性,虽然高手还能通过一些手段看得出来,但是比什么都不做还是要好多了。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • URL 重定向
  • 使用相对链接
  • 清理 HTML Head 中没用的代码
  • 大功告成
相关产品与服务
网站建设
网站建设(Website Design Service,WDS),是帮助您快速搭建企业网站的服务。通过自助模板建站工具及专业设计服务,无需了解代码技术,即可自由拖拽模块,可视化完成网站管理。全功能管理后台操作方便,一次更新,数据多端同步,省时省心。使用网站建设服务,您无需维持技术和设计师团队,即可快速实现网站上线,达到企业数字化转型的目的。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档