出于保密和安全方面的原因,一些用戶會(huì)考慮把網(wǎng)站所用的后臺(tái)隱藏起來(lái),這中需求有一個(gè)收費(fèi)插件叫「hide my wp」,如果你沒(méi)有辦法購(gòu)買(mǎi),或者不想使用插件,這篇文章就是為你準(zhǔn)備的。本文中的方法適合有一定動(dòng)手能力的用戶使用。下面我們就來(lái)看看怎么一步一步地把 WordPress 隱藏掉。
URL 重定向
WordPress 的目錄結(jié)構(gòu)是最具特色的,如果不經(jīng)過(guò)處理,別人通過(guò)代碼一眼就能看出來(lái)你的網(wǎng)站是用的 WordPress,我們首先需要做的就是修改掉 WordPress 的目錄結(jié)構(gòu)。
/**
* 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);
}
}
//不重寫(xiě)多站點(diǎn)和自主體
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');
}
}
以上代碼假設(shè)在你的主題中有/assets/文件夾,如果你使用的是 Apache 服務(wù)器,WordPress 會(huì)自動(dòng)為你重建好重寫(xiě)需要的.htacces文件,如果你使用的是 Nginx,還需要手動(dòng)添加重寫(xiě)規(guī)則到你的主機(jī)配置文件中。
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;
}
上面的規(guī)則硬編碼了 /wp-content/ 目錄,如果你在主題中修改了 WP_CONTENT_URL 或 WP_CONTENT_DIR 常量,可能會(huì)出現(xiàn)沖突,確保以上代碼中的 wp-content 目錄是正確的就可以了。
使用相對(duì)鏈接
所有的地方都使用絕對(duì)鏈接也是 WordPress 的一大特點(diǎn),其實(shí)這是沒(méi)有必要的,我們通過(guò)下面的代碼可以吧絕對(duì)鏈接修改成為相對(duì)鏈接。
/**
* 修改絕對(duì)鏈接為相對(duì)鏈接
*
* 提取自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');
清理 HTML Head 中沒(méi)用的代碼
WordPress 在 <head> 中添加了很多我們平時(shí)用不到的代碼,這不但增加了垃圾代碼,對(duì)網(wǎng)站后臺(tái)系統(tǒng)也暴露得很充分,好在我們可以很容易的清理掉這些代碼,添加以上代碼到主題的 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');
大功告成
以上操作可以隱藏絕大多數(shù)的 WordPress 信息,大大提高 WordPress 的安全性,雖然高手還能通過(guò)一些手段看得出來(lái),但是比什么都不做還是要好多了。



https://www.kevinleary.net/hide-site-is-using-wordpress/
直接搬好像代碼有點(diǎn)問(wèn)題了。。