简体   繁体   English

更改Wordpress管理员URL

[英]Change Wordpress Admin URL

I changed my Wordpress directory structure quite a bit. 我改变了我的Wordpress目录结构。 Here's what I have: 这就是我所拥有的:

define('WP_SITEURL', 'http://' . $_SERVER['SERVER_NAME'] . '/wordpress');
define('WP_HOME',    'http://' . $_SERVER['SERVER_NAME']);
define('WP_CONTENT_DIR', dirname(__FILE__) . '/content');
define('WP_CONTENT_URL', 'http://' . $_SERVER['SERVER_NAME'] . '/content');

So I have a content directory which contains my Plugins and Themes. 所以我有一个内容目录,其中包含我的插件和主题。 And then I have a wordpress directory which contains the core WP files, minus the wp-content folder. 然后我有一个wordpress目录,其中包含核心WP文件,减去wp-content文件夹。

With this new structure, I have to access the WP backend with this URL: http://site.dev/wordpress/wp-admin 有了这个新结构,我必须使用以下URL访问WP后端: http://site.dev/wordpress/wp-adminhttp://site.dev/wordpress/wp-admin

Is there a way I can change it so I can just access it like so: http://site.dev/wp-admin 有没有办法我可以改变它,所以我可以这样访问它: http://site.dev/wp-adminhttp://site.dev/wp-admin

I don't want wordpress to be in the URL. 我不希望wordpress在URL中。 Would this be an htaccess update I need to make, or is there a setting I can use in my wp-config.php file? 这是我需要做的htaccess更新,还是我可以在我的wp-config.php文件中使用的设置?

Here's an article from wordpress's site. 这是来自wordpress网站的一篇文章。

http://wordpress.org/support/topic/how-to-change-the-admin-url-or-wp-admin-to-secure-login http://wordpress.org/support/topic/how-to-change-the-admin-url-or-wp-admin-to-secure-login

  1. Add constant to wp-config.php 将常量添加到wp-config.php

     define('WP_ADMIN_DIR', 'secret-folder'); define( 'ADMIN_COOKIE_PATH', SITECOOKIEPATH . WP_ADMIN_DIR); 
  2. Add below filter to functions.php 将下面的过滤器添加到functions.php中

     add_filter('site_url', 'wpadmin_filter', 10, 3); function wpadmin_filter( $url, $path, $orig_scheme ) { $old = array( "/(wp-admin)/"); $admin_dir = WP_ADMIN_DIR; $new = array($admin_dir); return preg_replace( $old, $new, $url, 1); } 
  3. Add below line to .htaccess file 将以下行添加到.htaccess文件中

     RewriteRule ^secret-folder/(.*) wp-admin/$1?%{QUERY_STRING} [L] 

I played around with this and there is a much simpler way to do this all in this one simple function below without having to muck around with anything else (create unnecessary folders, redirects, pages, etc.). 我玩了这个,并且有一个更简单的方法可以在下面的这个简单的功能中完成所有操作,而不必乱丢其他任何东西(创建不必要的文件夹,重定向,页面等)。

// Simple Query String Login page protection
function example_simple_query_string_protection_for_login_page() {

$QS = '?mySecretString=foobar';
$theRequest = 'http://' . $_SERVER['SERVER_NAME'] . '/' . 'wp-login.php' . '?'. $_SERVER['QUERY_STRING'];

// these are for testing
// echo $theRequest . '<br>';
// echo site_url('/wp-login.php').$QS.'<br>';   

    if ( site_url('/wp-login.php').$QS == $theRequest ) {
        echo 'Query string matches';
    } else {
        header( 'Location: http://' . $_SERVER['SERVER_NAME'] . '/' );
    }
}
add_action('login_head', 'example_simple_query_string_protection_for_login_page');

This is very helpful topic. 这是非常有用的话题。 I made some little correction in the function and this is my version: 我在函数中做了一些小修正,这是我的版本:

add_filter('site_url',  'wpadmin_filter', 10, 3);

 function wpadmin_filter( $url, $path, $orig_scheme ) {
    $request_url = $_SERVER['REQUEST_URI'];

    $check_wp_admin = stristr($request_url, 'wp-admin');
    if($check_wp_admin){
        wp_redirect( home_url( '404' ), 302 );
        exit();
    }

    $old  = array( "/(wp-admin)/");
    $admin_dir = WP_ADMIN_DIR;
    $new  = array($admin_dir);
    return preg_replace( $old, $new, $url, 1);
 }

Mainly for redirecting of wp-admin . 主要用于重定向wp-admin

And most important part: 最重要的部分:

add_rewrite_rule( '^' . 'backend/(.*)','wp-admin/$1?%{QUERY_STRING}' );

To updates .htaccess rule. 更新.htaccess规则。

All I did was moved /wp-admin folder ( inside of public_html/wordpress ) into public_html and I double checked to make sure it was going to work by renaming my WordPress folder ( I used wordpress_test , you can use anything ) and went to my site example.com/wp-admin - it worked just the same as if I went to example.com/wordpress/wp-admin . 我所做的只是将/wp-admin文件夹(在public_html/wordpress )移动到public_html ,我仔细检查以确保它通过重命名我的WordPress文件夹(我使用wordpress_test ,你可以使用任何东西)来工作,然后去了我的site example.com/wp-admin - 它就像我去example.com/wordpress/wp-admin

The only thing which is quite tricky is changing the wp-admin to something else , due to the fact the WP had coded the wp-admin throughout numerous files. 唯一非常棘手的是将wp-admin改为其他东西,因为WP已经在很多文件中编写了wp-admin Just simply changing the name causes php and other errors. 只是简单地更改名称会导致php和其他错误。 Simple plugin I find will fix that easily. 我发现的简单插件可以轻松解决这个问题。

Note : I didn't have to make any coding to do this. 注意我没有做任何编码来做到这一点。 I had to change some code around for the wp-admin , because the plugin didn't do what I wanted. 我不得不为wp-admin更改一些代码,因为插件没有按我想要的那样做。

There is one other way that will ensure quite better tactic to your secured wp-admin. 还有另一种方法可以确保您的安全wp-admin更好的策略。

As well having own wp-admin name as perhaps: "worksersneeded/" 也许有自己的wp-admin名称:“worksersneeded /”

I did it to one of my sites, ended up in miracle where the probed SSL site was displaying different towards my site from different geo locations. 我在我的一个网站上做到了,最终出现了奇迹,探测到的SSL网站在不同的地理位置显示不同的网站。

You will have to download a tool called Notepad ++: https://notepad-plus-plus.org/download/ 您必须下载名为Notepad ++的工具: https//notepad-plus-plus.org/download/

Unless you will be doing to much work on each of the files in the directory. 除非您要对目录中的每个文件进行大量工作。

After then you need to extract the WordPress into a folder. 之后,您需要将WordPress提取到一个文件夹中。

Then edit all files in the directory while searching after wp-admin. 然后在wp-admin之后搜索时编辑目录中的所有文件。 Then replace all the files with your the name: "workersneeded" or your own name. 然后用您的名字替换所有文件:“workersneeded”或您自己的名字。

Like in notepad enter "search in files" to "find what": wp-admin/ 在记事本中输入“在文件中搜索”以“找到什么”:wp-admin /

And in "replace with": workersneeded/ 并在“替换为”:工人需要/

Then replace all files. 然后替换所有文件。

You need to put into wp-config.php this line as well to monitor all problems: 您还需要将此行放入wp-config.php以监控所有问题:

ini_set('log_errors',TRUE);
ini_set('error_reporting', E_ALL);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');

After you overwritten most in the WordPress directory and your wp-admin has now the name "workersneeded" you will most likely encounter slight problems with some of your WordPress plugins or themes. 在WordPress目录中覆盖了大部分内容后,你的wp-admin现在名称为“workersneeded”,你很可能会遇到一些WordPress插件或主题的轻微问题。

That is why you will need to log them into the error_log.txt file. 这就是您需要将它们记录到error_log.txt文件中的原因。

After finding the errors in the file. 找到文件中的错误后。 You will most likely need to edit the .php file which still tries to connect to wp-admin. 您很可能需要编辑仍然尝试连接到wp-admin的.php文件。 That way you can replace the file information of wp-admin to your administration folder. 这样您就可以将wp-admin的文件信息替换为您的管理文件夹。

You can again download your plugins and replace the same procedure as above with notepad++. 您可以再次下载插件并使用notepad ++替换上述相同的过程。 That way you can make all plugins available with the new folder name. 这样,您可以使用新文件夹名称使所有插件可用。

Then upload the folder of each plugin into your wp-content directory with wp file manager plugin. 然后使用wp文件管理器插件将每个插件的文件夹上传到wp-content目录中。

Be aware that your WordPress can't be auto-updated or updated doing so, even doing re-installation of your WordPress. 请注意,即使重新安装WordPress,也无法自动更新或更新WordPress。 You will have to do these replacements each time. 您每次都必须进行这些替换。

This was done with 4.9.8 version of WordPress as well as the newest 5.0.3 这是使用4.9.8版本的WordPress以及最新的5.0.3完成的

With 5.0.3 you get more errors into the error_log.txt file. 使用5.0.3,您可以在error_log.txt文件中获得更多错误。 Unknown why. 不明原因。

Finally found a way to do it without a plugin AND WITHOUT MODIFYING WP CORE (all tutorials suggests to do so for some weird reason). 终于找到了一种方法,可以在没有插件的情况下完成它并且无需修改WP CORE (所有教程都建议这样做有些奇怪的原因)。

1- Copy wp-login.php and rename it to new-secret-url.php (on your root directory) 1-复制wp-login.php并将其重命名为new-secret-url.php (在根目录下)

2- Open new-secret-url.php file and perform a search/replace of wp-login.php to new-secret-url.php 2-打开new-secret-url.php文件并执行搜索/替换wp-login.phpnew-secret-url.php

3- Add the following code to your functions.php: 3-将以下代码添加到functions.php:

/** Hide default login */
add_action( 'init', 'marounmelhem_hide_login' );
function marounmelhem_hide_login() {

    //Only proceed for guests
    if ( ! is_user_logged_in() ) {

        //Getting current page
        $current_url   = str_replace( '/', '', $_SERVER['REQUEST_URI'] );
        $hiddenWpAdmin = 'new-secret-url'; //Change this to your new secret wp-admin url
        $redirectNaTo  = '/';

        //Checking if accessing correct login url
        if ( $current_url == $hiddenWpAdmin ) {
            wp_redirect( '/'.$hiddenWpAdmin.'.php' );
            exit;
        }

        //Only allow requests to wp-login.php coming from correct login url
        $adminToCheck = [
            'wp-admin',
            'wp-login.php'
        ];
        if (
            in_array( $current_url, $adminToCheck )
            &&
            $_GET['action'] !== "logout"
        ) {
            wp_redirect( $redirectNaTo );
            exit();
        }
    }
}

4- This only works if you're not using any other frontend login forms, if you do, you can change: 4-这仅适用于您未使用任何其他前端登录表单的情况,如果您这样做,您可以更改:

is_user_logged_in() to possibly !current_user_can( 'subscriber' ) (or the role given in the frontend login logic) is_user_logged_in()可能!current_user_can( 'subscriber' ) (或前端登录逻辑中给出的角色)

5- Not sure if ajax calls works with the above, please let me know if you've tried it 5-不确定ajax调用是否适用于上述情况,如果你已经尝试过,请告诉我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM