Let’s take this WordPress site “www.draftposts.com” as an example. What we want to achieve is to host the admin panel (“www.draftposts.com/wp-admin/”) on a different (sub)domain (making it “admin.draftposts.com/wp-admin/” for example).
Mainly there are two parts.
- Add a web server configuration for the new (sub)domain.
- Modify “wp-config.php”
Our web server is Nginx. We can just copy the configuration for “www.draftposts.com” and modify the “server_name” to “admin.draftposts.com”.
For example, the original configuration is:
server { server_name www.draftposts.com; root /var/www/www.draftposts.com; index index.php; # Additional rules ... }
We copy this, modify the “server_name”, and it becomes:
server { server_name admin.draftposts.com; root /var/www/www.draftposts.com; index index.php; # Additional rules ... }
Notice that we only changed the “server_name” part, anything else (for example the “root” directive) remains untouched. If your site uses SSL/TLS, you may need to change the “ssl_certificate” and “ssl_certificate_key” directives as well.
After modifying the configuration file, tell Nginx to test the new config and reload using the command:
nginx -t && nginx -s reload
If there is no error, “admin.draftposts.com” should be accessible now. However, WordPress will try to redirect visitors to “www.draftposts.com” still. We need to modify “wp-config.php” or more specifically, define the “WP_SITEURL” constant.
Add this to “wp-config.php”:
if ( isset( $_SERVER['SERVER_NAME'] ) && 'admin.draftposts.com' === $_SERVER['SERVER_NAME'] ) { define( 'WP_SITEURL', 'http://' . $_SERVER['SERVER_NAME'] ); }
Replace ‘admin.draftposts.com’ with your own (sub)domain. Change “http://” to “https://” if you use SSL/TLS.
WordPress admin panel can be accessed from “admin.draftposts.com/wp-admin/” now. Goal achieved.
Why do you do this?
Well, I did this to separate the main site and the admin site, so that additional security measures can be taken specifically to protect the admin site, without causing too much trouble.
This may also be useful for those who find their WordPress sites “outgrowing a single server“.
PHP Code modified from xiidea’s answer on StackExchange.
1 thought on “How to Host wp-admin on a Different Domain”