maksim lev
maksim lev

Reputation: 11

How to make the wp-admin access from a different subdomain?

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

--------- 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;
        }

}

Upvotes: 1

Views: 889

Answers (2)

Sevy
Sevy

Reputation: 75

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"

Upvotes: 0

Richard Smith
Richard Smith

Reputation: 49812

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.

Upvotes: 0

Related Questions