出于安全原因,我试图通过重写链接来限制我的 wordpress 站点管理员和登录面板对非管理员用户的访问,这样如果用户输入 http://www.mysite.com/wp-login.php或 http://www.mysite.com/wp-admin他被重定向到错误 404 页面,但如果他输入 http://www.mysite.com/blah-login或 http://www.mysite.com/blah-admin重定向到我的 WP 管理员或登录面板。我有以下选择来做到这一点。
register_activation_hook( __FILE__, 'activate' );
function activate() {
rewrite();
flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'deactivate' );
function deactivate() {
flush_rewrite_rules();
}
add_action( 'init', 'rewrite' );
function rewrite() {
add_rewrite_rule( 'blah-admin/?$', 'wp-admin', 'top' );
add_rewrite_rule( 'blah-login/?$', 'wp-login.php', 'top' );
add_rewrite_rule( 'blah-register/?$', 'wp-register.php', 'top' );
}
它完美运行,唯一的问题是它不限制对 wp-admin、wp-login.php 或 wp-registe.php 的访问(这是必须的)。
AuthUserFile /dev/null
AuthGroupFile /dev/null
AuthName "Wordpress Admin Access Control"
AuthType Basic
<LIMIT GET>
order deny,allow
deny from all
allow from xxx.xxx.xxx.xxx
</LIMIT>
并将其放在 wp-admin 文件夹下,它有两个缺点,一个是它只会限制对我的 wp-admin 文件夹的访问,而不是 wp-register.php 或 wp-login.php,第二个是我是 DHCP 客户端,所以
allow from xxx.xxx.xxx.xxx
不会为我工作。 那么我的问题真的有解决方案吗?
最佳答案
如果他们尚未登录,我使用此代码段将人们重定向到远离后端。您可以修改它以指向您的 404:
// BLOCK BACKEND ACCESS FOR NON-ADMINS
add_action( 'init', 'blockusers_init' );
function blockusers_init() {
// If accessing the admin panel and not an admin
if ( is_admin() && !current_user_can('level_10') ) {
// Redirect to the homepage
wp_redirect( home_url() );
exit;
}
}
只需更改
home_url()
中的 URL在 wp_redirect
下为您的 404 页面添加功能.
关于.htaccess - 当非管理员尝试访问 wp-admin 或 wp-login.php 时,如何将用户重定向到 404 页面未找到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18324307/