1

I would like to redirect example.com/wp-admin to my index and make the WP-Admin accessible though something like example.com/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 ?
Also need to access example.com/wp-admin/index.php as example.com/admin/index.php, example.com/wp-admin/posts.php as example.com/admin/posts.php etc..

Shijo Rose
  • 193
  • 5
  • 17

3 Answers3

2

In my opinion if you use plugin than it will find security holes in your WordPress.

You can use the following steps to create your own URL of login either admin or else as follows:

1.) Add constant to wp-confing.php

define('WP_ADMIN_DIR', 'secret-folder');
define( 'ADMIN_COOKIE_PATH', SITECOOKIEPATH . WP_ADMIN_DIR);

2.) Add below filter to 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 if you are using Apache

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

Edit the Nginx virtual host file and place the following:

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

Done...!!!

Now your admin URL will be like: http://www.yourdomain.com/secret-folder/

and the below code will restrict the URL: site.com/wp-admin

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)){
    $redirect_to = $_REQUEST['redirect_to'];
    $check_wp_admin = stristr($redirect_to, 'wp-admin');
    if($check_wp_admin){
      wp_safe_redirect( '404.php' );
    }
  }
}

Edited: Edit the Nginx virtual host file and place the following:

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

In case if you want to redirect url from index.php to back on admin

server {
  index index.php;

  if ($request_uri ~* "^(.*/)index\.php$") {
    return 301 $1;
  }

  location = /admin/index.php {
    return 301 $scheme://www.example.com/admin/;
  }
}

Another benefit from doing it this way is that nginx does a return faster than a rewrite.

czerasz
  • 13,682
  • 9
  • 53
  • 63
Deep Kakkar
  • 5,831
  • 4
  • 39
  • 75
  • It's works when i was using `Appache`, but now i moved to `NGINX`. Then it's not not working now. – Shijo Rose Jun 04 '15 at 07:05
  • The preferred method is to use NO IF-STMTS: Try with # Login Short Cut location ~* /login/ { rewrite ^/login/(.*)? /wp-admin/$1; } – Deep Kakkar Jun 04 '15 at 07:08
  • it's already applied. it works only for entering `example.com/admin/`. no others like, if we enter `example.com/admin/index.php` it return "Not found" – Shijo Rose Jun 04 '15 at 07:16
  • what are you getting with admin/index.php , 404 or else ? I have a link for you http://stackoverflow.com/questions/10591258/remove-index-php-from-url-only-on-root-with-nginx-rewrite hope this help. – Deep Kakkar Jun 04 '15 at 07:24
  • Not working anymore. Checked at both apache and nginx – wp student Jun 11 '16 at 06:37
  • Has anyone got the admin subfiles working like posts.php, edit.php, upload.php to not show an nginx 404? Or do we need to manually specify the admin URLs in an external conf file – George Nick Gorzynski Apr 27 '23 at 00:11
0

You mean to change the url of wp-admin. Then you can use a plugin mentioned below Wordpress Link

After installing the plugin Under settings->Permalink we can change WP-admin page slug to what we need.

Rahul S
  • 11
  • 3
-1

For changing the admin url ,Try using the below plugin https://wordpress.org/plugins/wordfence/

sarath
  • 698
  • 6
  • 20