Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess 当非管理员尝试访问wp admin或wp-login.php时,如何将用户重定向到404页面未找到错误_.htaccess_Wordpress - Fatal编程技术网

.htaccess 当非管理员尝试访问wp admin或wp-login.php时,如何将用户重定向到404页面未找到错误

.htaccess 当非管理员尝试访问wp admin或wp-login.php时,如何将用户重定向到404页面未找到错误,.htaccess,wordpress,.htaccess,Wordpress,出于安全原因,我试图通过重写链接限制我的wordpress站点管理员和登录面板对非管理员用户的访问,例如,如果用户键入或重定向到错误404页面,但如果他键入或重定向到我的WP管理员或登录面板。我有以下几种选择 重写我不擅长的.htaccess文件,我不想弄乱我网站的.htaccess文件 使用$wp_rewrite类,我编写了一个小插件,其代码如下 register_activation_hook( __FILE__, 'activate' ); function activate() {

出于安全原因,我试图通过重写链接限制我的wordpress站点管理员和登录面板对非管理员用户的访问,例如,如果用户键入或重定向到错误404页面,但如果他键入或重定向到我的WP管理员或登录面板。我有以下几种选择

  • 重写我不擅长的.htaccess文件,我不想弄乱我网站的.htaccess文件
  • 使用$wp_rewrite类,我编写了一个小插件,其代码如下

    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(必须)的访问

  • 我可以将以下规则写入新的.htaccess文件

    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>
    
    AuthUserFile/dev/null
    AuthGroupFile/dev/null
    AuthName“Wordpress管理员访问控制”
    AuthType Basic
    命令拒绝,允许
    全盘否定
    允许从xxx.xxx.xxx.xxx开始
    
    把它放在wp admin文件夹下,它有两个缺点:一是它只会限制对我的wp admin文件夹的访问,而不是wp-register.php或wp-login.php;二是我是一个DHCP客户端,所以
    允许来自xxx.xxx.xxx的访问将不适用于我

  • 我可以使用第二条和第三条规则的组合,但它肯定不起作用,因为我不能为整个被阻止的文件夹提供替代永久链接

  • 作为最后的手段,我可以使用wp-modal插件的permalink重写功能,它的工作方式很有魅力,但这个插件与我的主题不兼容


  • 那么我的问题真的有解决办法吗?

    我想你是在试图保护自己免受暴力攻击吧?为什么不限制允许登录尝试的频率?有一个非常可靠的插件叫做“限制登录尝试”,它将跟踪每个IP和cookie的使用情况

    在这里查看:

    不过,试图屏蔽登录页面是一个有趣的想法。您可以在某个地方创建自己的登录页面,并让它与您编写的自定义登录脚本交互。这将允许301将登录页面重定向到404页面,而不会影响任何核心功能

    您可以在
    https://yourwpsite.com/supersecretlogin
    然后将其
    发布到使用
    wp\u signon
    方法的自定义处理程序。下面是一个如何在不使用wp admin的情况下登录用户的示例:

    $creds = array();
    $creds['user_login'] = $_POST['user'];
    $creds['user_password'] = $_POST['password'];
    $creds['remember'] = true;
    $user = wp_signon( $creds, false );
    if ( is_wp_error($user) )
       exit($user->get_error_message());
    header('Location: https://yourwpsite.com/wp-admin');
    


    希望有帮助

    如果用户尚未登录,我使用此代码段将其从后端重定向。您可以将其修改为指向您的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;
        }
    }
    

    只需将URL从
    home\u URL()
    函数更改为
    wp\u redirect

    下的404页面,wp Codex有一个脚本限制非管理员的管理员访问,并且有一个条件允许访问Ajax请求,因为非管理员用户可能从站点前端发出这些请求。如果(!current_user_can('manage_options')&(!wp_doing_ajax()){…应该将其合并到脚本中吗?