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

郑重承诺丨优站网提供安全交易、信息保真!
增值服务:
自动发货
网盘下载
安装指导
环境配置
二次开发
网站建设
¥ 0 (VIP免费升级VIP开通VIP尊享优惠特权
立即下载 升级会员 购买教程 售后支持
详情介绍

出于保密和安全的考虑,有些站长会考虑隐藏网站使用的背景。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:1427707223
收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

免责声明   ⚠️ 1. 本站所有资源来源于网络收集,若资源侵犯了您的合法权益, 请来信通知我们(Email: 1427707223@qq.com),我们会及时删除,给您带来的不便,我们深表歉意! 2. 分享目的仅供大家学习和交流,若使用商业用途,请购买正版授权! 否则产生的一切后果将由下载用户自行承担! 3. 会员不得使用于非法商业用途,不得违反国家法律。否则后果自负! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 6. 因源码具有可复制性,一经购买 ,不得以任何形式退款。 7.更多详情请点击查看

优站网 WordPress教程 通过隐藏WordPress信息提高网站的安全性 https://www.zhanceo.com/20908.html

诚乃立身之本、信为道德之基

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

相关文章

联系官方客服

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

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