Report this

What is the reason for this report?

Wordpress Hide wp-admin with Nginx

Posted on January 23, 2015

I would like to redirect zombay.org/wp-admin to my index and make the WP-Admin accessible though something like zombay.org/admin. 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.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

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');
define('ADMIN_COOKIE_PATH', SITECOOKIEPATH . WP_ADMIN_DIR);

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

<?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() );
                        exit;
                }
        }
}

Edit the Nginx virtual host file and place the following:

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

Do a configtest and reload.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.