Wordpress Hide wp-admin with Nginx

Posted January 23, 2015 20k views

I would like to redirect to my index and make the WP-Admin accessible though something like
I did this in apache but after switching to nginx I don’t know how to do this. Is there a way to do this without changing any of the wordpress files but changing the nginx config ?

Any help is appreciated, thanks.

1 comment

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

I haven’t tested this solution thoroughly, so please check if it works for you.

Edit the wp-config.php file and place the following:

define('WP_ADMIN_DIR', 'admin');

Create a mu-plugins directory:

mkdir wp-content/mu-plugins

Create a file inside it and place the following code. Let’s name this file admin-rewrite.php

add_filter( 'site_url',  'wpadmin_filter', 10, 3 );
function wpadmin_filter( $url, $path, $orig_scheme ) {
        return preg_replace( "/(wp-admin)/", WP_ADMIN_DIR, $url, 1 );

add_action( 'login_form', 'redirect_wp_admin' );

function redirect_wp_admin(){
        $redirect_to = $_SERVER['REQUEST_URI'];

        if ( count( $_REQUEST ) > 0 && array_key_exists( 'redirect_to', $_REQUEST ) ) {
                $check_wp_admin = stristr( $_REQUEST['redirect_to'], 'wp-admin' );
                if( $check_wp_admin ) {
                        wp_redirect( home_url() );

Edit the Nginx virtual host file and place the following:

location ~* /admin/ {
    rewrite ^/admin/(.*) /wp-admin/$1 last;

Do a configtest and reload.

  • It works to the point that wp-admin gets redirected to the index, but chrome gives me the error ERR_TOO_MANY_REDIRECTS.

  • Paste your complete Nginx virtual host file in and share its URL.

  • without

    location ~* /admin/ {
        rewrite ^/admin/(.*) /wp-admin/$1 last;
  • I tried it again on a fresh Debian 7 VM and it does work for me. Check the following:

    • The define() statements in the wp-config.php file should be above the line /* That's all, stop editing! Happy blogging. */.
    • The Nginx location ~* /admin/ {[...]} directive should come after the location / {[...]} directive.
    • Make sure Nginx is reloaded.
    • If you have any plugins related to redirects disable them. It’d be better to disable all plugins and test this.