1

------- Technologies I'm using ---------

  • Debian 10
  • Nginx server
  • PHP8.0

--------- The scenario ------------

I have exmaple.com which has an HTML static website and I have a subdomain, blog.example.com I installed WP on it and it's working fine but I want to make the wp-admin access on login.blog.example.com.

-------- What I tried -------------

1- I tried redirecting any /wp-* URL to login.blog.example.com, but that isn't useful if there are no files/folders of wp-admin on login.blog.example.com.

2- I followed this, but it wasn't handy since they are redirecting to 404 and a static page https://403.ie/how-to-serve-wp-admin-from-a-separate-subdomain/

-------- The nginx configuration -----------

example.com:

server{
        listen 80;
        listen [::]:80;
        server_name IP_ADDRESS;
        return 301 http://example.com;
}

server {
        listen 80;
        listen [::]:80;

        server_name example.com;

        root /var/www/example.com;
        index index.html;

        location / {
                try_files $uri $uri/ =404;
        }
}

blog.example.com:

server {
        listen 80;
        listen [::]:80;

        server_name blog.example.com;

        root /var/www/blog.example.com;
        index index.html index.php index.htm;

        location = /favicon.ico { log_not_found off; access_log off; }
        location = /robots.txt { log_not_found off; access_log off; allow all; }
        location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
                expires max;
                log_not_found off;
        }

        location / {
                #try_files $uri $uri/ =404;
                try_files $uri $uri/ /index.php$is_args$args;
        }

#       location /wp-admin{
#               rewrite ^/wp-(.*)$ http://login.blog.example.com redirect;
#       }

        location ~ \.php$ {
                try_files $uri /index.php;
                include fastcgi_params;
                fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }

}
maksim lev
  • 11
  • 3

2 Answers2

0

The first step is to create a server to handle requests to login.blog.example.com. This is identical to blog.example.com except for the server_name and any "login" bits you want to add.

Then add a redirect in the blog.example.com server block:

location ^~ /wp-admin { 
    return 302 $scheme://login.blog.example.com$request_uri;
}

WordPress may insist on redirecting you back to blog.example.com. This is because of the settings for site URL in the Dashboard configuration or the wp-config.php configuration file.

You should be able to drop the hostname from the HOME and SITEURL values and use / instead of http://blog.example.com.

Richard Smith
  • 45,711
  • 6
  • 82
  • 81
0

Maybe I'm wrong but I'd suggest this:

location ~ ^/wp-admin { 
    return 302 $scheme://login.blog.example.com$request_uri;
}

As far as I know the ^ char is supposed to go before the regular expression, according to UNIX REGEX standards.

So now the expression sounds like "whatever uri starts with /wp-admin"

Sevy
  • 75
  • 1
  • 3
  • Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. **Would you kindly [edit] your answer to include additional details for the benefit of the community?** – Jeremy Caney Jul 14 '23 at 17:07