通过隐藏WordPress信息提高网站的安全性
最近更新 2022年04月05日
资源编号 20908

通过隐藏WordPress信息提高网站的安全性

2021-11-19 WordPress教程 0 1,053
下单流程:
选择资源
赞助获取
网盘下载
额外(二次开发 网站建设)
¥ 0牛币

VIP免费升级VIP

立即下载 升级会员 下单教程 售后支持
🔊 下载须知
下载前请认真阅读本重要提示:(如下载链接失效或支付未回调,请联系站长QQ972908224或在网站发私信给站长,信息一定要有:网站会员名称、购买金额、资源链接,见字后会第一时间处理,晚上23点至早上8点期间是站长休息时间,留言即可,请勿频繁发信息。本站以用户的需求为基础,将最有价值的信息和资源分享给广大编程人员及源码爱好者在本地电脑端进行调试、参考和研究,本站不提供任何技术服务!所有资源禁止在服务器或者虚拟机下进行联网搭建运营,禁止任何商业环境使用!所有资源展示图片和描述不代表本站的立场!本站只是存储平台!如需运营请购买正版!杜绝利用本站资源进行触犯国家法律法规,下载者如触犯以上声明,所产生的一切后果由下载者自行承担!请悉知!其次,源码模板过多不能保证每个细节都符合你的需求,也可能存在未知的BUG与瑕疵、残次和版本过旧、破解版本过期等问题。你下载的资源和程序源码组件因其特殊性均为可复制品,所以不支持任何理由的退款兑现;请认真阅读本站声明和相关条款,谨慎斟酌后再下载
详情介绍

出于保密和安全的考虑,有些站长会考虑隐藏网站使用的背景。WordPress 系统中有一个叫「hide my wp」的插件。如果你不想购买或使用插件,那么这篇文章就是为你准备的。本文的方法适合有一定动手能力的朋友参考。下面,站长会一步步教你隐藏 WordPress 信息。

通过隐藏WordPress信息提高网站的安全性1.网址重定向
WordPress 的目录结构是最有特色的。如果不处理,别人一眼就能通过代码看出你的网站在使用 WordPress。我们首先要做的就是修改 WordPress 的目录结构。

/**
 * 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');
    }
}

上面的代码假设您的主题中有一个/assets/文件夹。如果你使用的是 Apache 服务器,WordPress 会自动重建。重写所需的文件。如果使用的是 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/目录硬编码的。如果修改主题中的 WP_CONTENT_URL 或 WP_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');

3.清理 HTML Head 中无用的代码
WordPress 添加了很多我们平时不常用的代码,不仅增加了垃圾代码,还暴露了网站的后端系统。幸运的是,我们可以轻松清理这些代码,并将上述代码添加到主题的functions.php文件中。

/**
 * 清理 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 && $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 信息,大大提高 WordPress 系统的安全性。虽然大师可以通过一些手段看到,但总比什么都不做好得多。

资源下载此资源仅限注册用户下载,请先
客服 QQ:972908224

本文章已结束,如转载请注明:汇站网 » 通过隐藏 WordPress 信息提高网站的安全性

温馨提示: (本资源会持续更新),若下载链接失效,请在下方 (留言)(私信)站长处理!
收藏 (0)

微信支付 微信扫一扫

支付宝支付 支付宝扫一扫

打赏二维码
点赞 (0)

站长资源下载中心-找源码上汇站

常见问题
  • 如果付款后没有弹出下载页面,多刷新几下,有问题联系客服!
查看详情
  • 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
查看详情

相关文章

联系官方客服

为您解决烦忧 - 24小时在线 专业服务