6

I am trying to set up multiple Wordpress sites in sub-folders under our domain (ie not multi-site), but I have difficulty configuring the REST API endpoints. For example, this endpoint works fine:

https://example.com/site1/?rest_route=/wp/v2/posts

But this endpoint gives a 404:

https://example.com/site1/wp-json/wp/v2/posts

I have tried to rewrite the failing url to the working url with these rules in my nginx configuration:

location /site1/wp-json {
    rewrite ^/site1/wp-json(.*)$ /site1/?rest_route=$1;
}

location /site1/ {
   try_files $uri $uri/ /site1/index.php$is_args$args;
}

I can't see any special handling of wp-json in the WordPress docs or the nginx wiki. What am I missing here? The permalinks for the site is set to Numeric (https://example.com/site1/archives/123) if that might play a role.

Update

Gist of the redacted full config file and the config syntax lints okay:

nginx -c /etc/nginx/nginx.conf -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Jannie Theunissen
  • 28,256
  • 21
  • 100
  • 127
  • Can you show the full config? – Tarun Lalwani Jul 10 '19 at 16:14
  • I've seen the rewrite line with the last flag at the end. Maybe that is what is missing? – Nicolai Grossherr Jul 10 '19 at 20:57
  • Thanks @TarunLalwani I have just tried with adding last and also rearranging the order of the two rewrite rules, but it didn't fix the 404 – Jannie Theunissen Jul 11 '19 at 07:04
  • 1
    @JannieTheunissen, I would like to see the full config to be able to comment on something. Currently, just these two lines don't help me – Tarun Lalwani Jul 11 '19 at 07:35
  • A gist of the full config coming up. I just need to redact a bunch of things in there – Jannie Theunissen Jul 11 '19 at 17:36
  • @TarunLalwani I added the full config – Jannie Theunissen Jul 16 '19 at 09:59
  • Do you need both rewrite rules? Why not just the location /site1/. That should set the pretty permalinks for the whole install, including the wp-json endpoints, no? As opposed to trying to rewrite the root, and a subdirectory. – DubVader Jul 16 '19 at 18:28
  • @DubVader I agree that logically we shouldn't need both, but I added the specific wp-json one because that route fails. Maybe it is caused by some of the other multi-site rules in the config? I just don't know enough nginx to spot it. – Jannie Theunissen Jul 17 '19 at 06:08
  • @JannieTheunissen [debug rewrite](https://serverfault.com/questions/333048/best-way-to-debug-nginx-config-file) may help – 鄭脈龍 Jul 17 '19 at 06:54
  • @JannieTheunissen writing the pretty permalinks should effect the entire install, including the endpoints. That is why your other endpoint call works with the query string, which is the default. I also noticed you have an "$is_args$args" as part of it. This could be fine, but if you just tried "?$args" instead. – DubVader Jul 17 '19 at 14:16
  • @JannieTheunissen, lets discuss this over the gist so commenting code is easier – Tarun Lalwani Jul 17 '19 at 16:54
  • @JannieTheunissen did you ever get it to work? Running into the same issue. Odd thing is that I was able to get custom routes working with a couple of different methods: `rewrite ^/wordpress/wp-json/(.*?)$ /wordpress/index.php?rest_route=/$1 last;` and/or `try_files $uri $uri/ /wordpress/index.php?$args /wordpress/index.php?q=$uri&$args;` But default routes like `wp-json/wp/v2/pages` return an empty array. – geochanto Oct 02 '19 at 02:28

4 Answers4

4

I just hit this too, in WP 5.7. Without pretty permalinks, ie with the "Plain" option like ?p=123, my nginx WP installation uses requests like:

/index.php?rest_route=/wp/v2/users/&who=authors...

And these all work fine.

However if I enable pretty permalinks, eg "Post name", /sample-post/, it starts making requests like:

/wp-json/wp/v2/users/?who=authors...

And these all return a 404. For example, editing or publishing posts fails, and browser devtools shows a string of 404s in this format.

But now we know the pattern that works, a solution is clear - we just need to map the not-working format to the working format:

# Resolves WP Gutenberg 404 issue
location /wp-json {
    rewrite ^/wp-json(.*)$ /index.php?rest_route=$1 last;
}
Don't Panic
  • 13,965
  • 5
  • 32
  • 51
2

I was able to resolve it like this:

location /wordpress/ {
  rewrite ^/wordpress/wp-json/(.*?)$ /wordpress/index.php?rest_route=/$1 last;
}
geochanto
  • 972
  • 2
  • 13
  • 45
  • 1
    I somehow missed your answer before posting mine - this works for me, +1. I'm leaving my answer as I go into more detail about how/why it works, which might be useful to others. I also think a `location` that more specifically targets just the URIs we want to modify would be better. – Don't Panic Feb 24 '22 at 01:25
1

I believe that the rewrite directive should be written as shown below:

server {
    location /site1/wp-json
        {
            rewrite ^(/site1/wp-json.*)$ /site1/?rest_route=$1 last;
        }
}
Faham Shaikh
  • 983
  • 2
  • 6
  • 15
  • Thanks Faham. I tried your solution, but it doesn't work. – Jannie Theunissen Jul 16 '19 at 09:58
  • 1
    Can you please posts the results by running `nginx -c /etc/nginx/nginx.conf -t` in your terminal. – Faham Shaikh Jul 16 '19 at 17:57
  • @JannieTheunissen there should be a site-enabled folder in your /etc/nginx can you check if there is any configuration file there? and if so, add the configurations over in that file. – Faham Shaikh Jul 17 '19 at 19:44
  • There is two included configurations which I have merged into the gist to get the whole picture – Jannie Theunissen Jul 18 '19 at 08:30
  • I know this is a very stupid question but after pondering for hours on your configuration file I am only left with the dumbest question of all. What is your website permalink setting? – Faham Shaikh Jul 18 '19 at 18:36
  • Thanks for your effort Faham. The permalink setting is `Numeric` (see question) – Jannie Theunissen Jul 19 '19 at 11:19
  • I thought so, its often the simplest of the things. Can you change your permalink to postname and check again. – Faham Shaikh Jul 19 '19 at 15:02
  • I tried setting permalinks to postname and I still have the same problem. I saw numerous answers on SO where they suggest setting the permalinks to anything other than the default to resolve getting 404 on WordPress ajax calls – Jannie Theunissen Jul 19 '19 at 15:26
  • Numeric permalink are never going to work as the rewrite will only work if pretty permalink are on. – Faham Shaikh Jul 19 '19 at 15:30
0

An easy way if your website pages in the subfolder is already working, just add index.php to the url:

https://site1.com/site2/index.php/wp-json/

If your website pages still doesn't work in the subfolder, add this code to nginx/sites-available/website.conf file too:

location /site2 {
        rewrite ^(/[^/]+)?(/wp-.*) /site2/$2 break;
        rewrite ^/site2/(.*)$ /site2/index.php?q=$1 last;
}
Fellipe Sanches
  • 7,395
  • 4
  • 32
  • 33