Wordpress Hide wp-admin with Nginx

  • Posted January 23, 2015

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.


Still haven’t found a solution after asking google numerous times, lol.

Submit an 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');

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.