Learn how to add a subdomain for your WordPress backend, separating it from your main site URL for enhanced security.
To add a separate subdomain URL for accessing your WordPress backend while keeping the default site URL for the frontend, you need to configure your server and WordPress settings accordingly. This setup allows you to access the WordPress admin dashboard through a subdomain like admin.example.com
, while your site remains accessible at example.com
. Below are the detailed steps to achieve this:
1. Create the Subdomain
a. Access Your Hosting Control Panel:
- Log in to your hosting control panel (such as cPanel, Plesk, or any custom panel provided by your hosting provider).
b. Navigate to Subdomain Management:
- Look for the Domains section and click on Subdomains.
c. Create a New Subdomain:
Enter the name of the subdomain (e.g.,
admin
).Ensure it points to the same document root as your main site (usually
public_html
or the directory where WordPress is installed).Click Create.
Note: If your hosting control panel allows you to specify the document root, set it to your WordPress installation directory.
2. Update DNS Records
a. Access DNS Zone Editor:
- In your hosting control panel, go to the DNS Zone Editor or equivalent.
b. Add an 'A' Record:
Create a new A Record for
admin.example.com
pointing to your server's IP address.If your hosting uses automatic DNS management, this step might be done automatically when you create the subdomain.
c. Propagate DNS Changes:
- Allow some time (usually up to 24 hours) for DNS changes to propagate globally.
3. Configure Your Web Server
For Apache Users:
a. Edit Virtual Host Configuration:
Access your server via SSH or use your hosting control panel's file manager.
Navigate to your Apache configuration files (usually in
/etc/httpd/conf.d/
or/etc/apache2/sites-available/
).
b. Add a ServerAlias:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com admin.example.com
DocumentRoot /path/to/wordpress
...
</VirtualHost>
c. Restart Apache:
- Run
sudo service httpd restart
orsudo service apache2 restart
.
For Nginx Users:
a. Edit Server Block Configuration:
- Navigate to Nginx configuration files (usually in
/etc/nginx/sites-available/
).
b. Add the Subdomain to server_name:
server {
listen 80;
server_name example.com www.example.com admin.example.com;
root /path/to/wordpress;
...
}
c. Restart Nginx:
- Run
sudo service nginx restart
.
4. Modify WordPress Configuration
a. Backup wp-config.php:
- Always backup configuration files before making changes.
b. Edit wp-config.php:
Locate your
wp-config.php
file in the root of your WordPress installation.Add the following code above the line that says
/* That's all, stop editing! Happy blogging. */
:
if (strpos($_SERVER['HTTP_HOST'], 'admin.example.com') !== false) {
define('WP_SITEURL', 'https://admin.example.com');
define('WP_HOME', 'https://example.com');
} else {
define('WP_SITEURL', 'https://example.com');
define('WP_HOME', 'https://example.com');
}
Explanation:
When you access your site via
admin.example.com
, WordPress will useadmin.example.com
as theWP_SITEURL
, which affects admin URLs.The
WP_HOME
remainsexample.com
, so the frontend URLs are consistent.
5. Update .htaccess or Nginx Configuration for Permalinks
For Apache Users:
a. Ensure .htaccess Exists:
- The
.htaccess
file should be in your WordPress root directory.
b. Default WordPress .htaccess Rules:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^admin\.example\.com$ [NC]
RewriteRule ^(wp-(admin|login\.php).*)$ $1 [L]
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
For Nginx Users:
a. Update Server Block:
- Add or modify the
try_files
directive:
location / {
try_files $uri $uri/ /index.php?$args;
}
6. Secure Both Domains with SSL
a. Obtain SSL Certificates:
- Use Let's Encrypt or another SSL provider to obtain certificates for both
example.com
andadmin.example.com
.
b. Configure SSL in Web Server:
- Update your Apache or Nginx configurations to include SSL settings for both domains.
7. Test the Setup
a. Access the Admin Subdomain:
Navigate to
https://admin.example.com/wp-admin/
.Log in with your WordPress admin credentials.
b. Verify Frontend Access:
- Visit
https://example.com
and navigate through your site to ensure everything works.
c. Check for Mixed Content Errors:
- Use browser developer tools to check for any mixed content warnings, which can occur if resources are loaded over HTTP instead of HTTPS.
8. Additional Considerations
a. Plugins and Themes Compatibility:
Some plugins or themes might have hardcoded URLs or may not function correctly with this setup.
Test all critical functionalities of your site.
b. Search Engine Indexing:
Use robots.txt to prevent search engines from indexing the admin subdomain.
Add the following to your
robots.txt
file:
User-agent: *
Disallow: /wp-admin/
c. Update Sitemap and SEO Settings:
- Ensure your XML sitemap and SEO plugins are configured to use the correct URLs.
9. Optional: Use a Plugin for Domain Mapping
If you prefer not to manually configure these settings, you can use a plugin to handle domain mapping.
a. Install a Domain Mapping Plugin:
- Plugins like Multiple Domain Mapping on Single Site can help manage multiple domains.
b. Configure the Plugin:
- Follow the plugin's documentation to map
admin.example.com
to your WordPress backend.
10. Troubleshooting
White Screen or Redirect Loops: This may occur due to incorrect settings in
wp-config.php
or server configurations.Clear Browser Cache: After making changes, clear your browser cache to ensure you're loading the latest configurations.
Check Error Logs: Review your server's error logs for any issues that can guide you in troubleshooting.
By following these steps, you can successfully add a subdomain for WordPress backend access, separating it from your default site URL. This setup enhances security by obscuring the admin login URL and can also help in organizing site management.