WordPress JSON API returns only 404 errors - json

I'm brand new to Wordpress and I would like to use the JSON API plugin.
So I put it in my plugin repository and I activated it but when I try to display the Json response by accessing the http://localhost/wordpress/wp-json/posts url I get a 404 error.
I'm probably missing something but according to the documentation it should be that simple. Any idea of what am I doing wrong?

It is an mod_rewrite issue.
Reason is one of these in your .htaccess:
not existing
wrong permissions
screwed up
Try the htaccess documentation on Wordpress for your .htaccess:
## BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Old question and answer, but for anyone coming here recently via search results (like me), /wp-json/posts should at least bring a JSON result (albeit still a 404 error), and /wp-json should list some available routes in JSON.
If it doesn't (e.g. it shows an Apache or other 404 error page), it's probably a permalinks issue

You have the wrong address. According to the documentation, the following are valid links:
Implicit mode examples:
http://www.example.org/?json=1
http://www.example.org/?p=47&json=1
http://www.example.org/tag/banana/?json=1
Explicit mode examples:
http://www.example.org/?json=get_recent_posts
http://www.example.org/?json=get_post&post_id=47
http://www.example.org/?json=get_tag_posts&tag_slug=banana
With user-friendly permalinks configured:
http://www.example.org/api/get_recent_posts/
http://www.example.org/api/get_post/?post_id=47
http://www.example.org/api/get_tag_posts/?tag_slug=banana
Source: https://wordpress.org/plugins/json-api/other_notes/
So in your case you should use http://localhost/wordpress/api/get_recent_posts/

/wp-json/elementor/v1/globals - 404 error
Just update the plugin if any update is available and deactivate it first and then re-activate it again. The problem will be solved.
When we activate the Elementor plugin then this plugin creates some essential files for editing, so when we will re-activate it then it will re-create all essential files and our problem will be solved. Thanks

It may depend on whether you have NGinx or Apache.
On some hosts with Plesk if Nginx is used, Plesk does not create or re-create the .htaccess file, but for WP it must be present.
If it's not there, just recreate it with the standard WP settings https://wordpress.org/support/article/htaccess/ as mentioned in one of the previous answers.
A workaround (insecure) is to create directories and files /wp-json/elementor/v1/globals with {} like Json standard, elementor will work the same, but I don't recommend doing that.

Related

Absolute URL in 100s of files rewrite

I have created a dump from WayBack Machine so that a client can view his older site in order to create content plan. I loaded the backup to /~xxxxxxxx/ but as all the css and js in 100s of files are coded as follows:
<link href="/skins/FixedSize/styleSheets/style.css"
This results in 404 because the /~xxxxxx/ is ignored.
I have tried the following but no luck.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? /~xxxxxxxx/ [R=301,L]
I need a way to achieve this without modifying 470 HTML files.
This probably is what you tried to express:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ /~xxxxxxxx/$1 [END,QSA]
However you should be aware that this might be in conflict with other content you publish on that http host. There probably is a reason why you created that folder in the first place instead of using a hosts DOCUMENT_ROOT which would have prevented the current issue.
Which leads to an alternate suggestion: why don't you simply use a separate http host, so another "subdomain" as it is sometimes called?
Anyway, if you want to give above implementation a try, then please also note these additional hints:
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Deployed laravel app, only homepage working

I have recently finished a website for a customer, the site works great locally. Currently only the homepage is working. However all links return a 500 server error.
This is 1and1, and I haven't heard many good things about it.
Currently I have the laravel folders for my app within a folder called webapp.
the domain name points to webapp/public.
I have also added the following line to my htaccess to get it to work
RewriteRule ^ /home.php [L]
which works for the homepage. I don't know much about the htaccess file, so I've messed around with a few other options such as
RewriteRule ^ /folios.php [L]
as well which is one of the links in my site, however this doesn't work.
My routes currently work like this
Route::get('/folios', 'AlbumsController#index');
However they return 500 server error
Change the line in the ".htaccess" file in the root folder from RewriteRule ^ index.php [L] to
RewriteRule ^ /index.php [L]
My suspicion is that you need to update the paths contained within public/index.php
Check out this article for instructions: https://medium.com/#Bohr/deploy-laravel-application-on-shared-hosting-godaddy-953758dc57ac
Did you run composer install on the server or copy out the vendor directory? You may also want to check that your htaccess file in the public directory contains the directives that are required (https://github.com/laravel/laravel/blob/master/public/.htaccess).
I did not realize that the server generated a new .htaccess file within the /webapp/ folder where I pointed the domain; I was editing the wrong .htaccess file

Whatever I do, .htaccess won't do the job

Working with .htacess file has always been a very frustrating experience for me. Someone please help.
This is what I want to achieve:
I am running Ubuntu 14.04.
Redirect my entire site (example.com) to a maintenance.html page.
Block everybody else except one IP, for example, I need to allow only 123.456.789.0
Here are my files:
Location of my index.html is /var/www/html
Location of my maintenance.html is /var/www/html
Location of my .htaccess file is /var/www/html
Contents of My .htaccess file:
#Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com[nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]
#301 Redirect Old File
Redirect 301 /index.html /maintenance.html
#Block users by IP
order allow,deny
deny from all
allow from 123.456.789.0
Please help me understand:
Is the location of each of the above files right? In what cases, the
page ends up in 500 internal server error?
What changes should I make in
/etc/apache2/apache.conf
/etc/apache2/sites-enabled/000-default.conf OR
/etc/apache2/sites-available/000-default.conf
Is is necessary to run a2enmod rewrite?
Should I add <IfModule mod_rewrite.c> and </IfModule> as header and footer in any of the above config files?
Sorry for too many questions, but I really want know it all this time.
Thanks in advance.
Is the location of each of the above files right? In what cases, the
page ends up in 500 internal server error?
A "500 Internal Server Error" message means there's an error and you're expected to check the server logs for the exact details. Apache will not display the error message to be seen by everyone.
What changes should I make
It depends on what the problem is. If the problem is "500 Internal Server Error" that means that we still don't know what the problem is.
Is is necessary to run a2enmod rewrite?
That command enables mod_rewrite. You need to enable it if it isn't enabled. You don't need to enable it if it's already enabled.
It's worth noting that this command is not part of official Apache distribution. Some Linux distros (namely Debian and derivatives) change third-party packages to match their configuration preferences, as in this case.
Should I add <IfModule mod_rewrite.c> and </IfModule> as header
and footer in any of the above config files?
As documentation explains, this block can be used to ignore directives when a given module is not installed. This can be useful for configuration templates to be distributed and optional features. In your case, it'll silently ignore your code if mod_rewrite is not available—you don't want that.
Last but not least:
order allow,deny
deny from all
allow from 123.456.789.0
... belongs to the old (and really hard to understand) Apache/2.2 syntax. If you are using Apache/2.4* you may want to try Require.
(*) Some distros hate bundling recent software but 2.4 has been around for several years
Thanks to #OlafDietsche and #ÁlvaroGonzález for this quick help. I am keeping their suggestions here so somebody like me will find it useful.
The problem is with my goals, not with the syntax. With their comments and answers, I came to know that my 2 goals were mutually contradicting ones.
I configured .htaccess to do both page-redirection and IP block. But if I am blocking (almost) everybody from accessing the site, page redirection makes no sense.
The required configuration in .htaccess is:
#Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com[nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]
#301 Redirect Old File
Redirect 301 /index.html /maintenance.html

Wordpress JsonAPI - /wp-json/ was not found on this server

I am using the following plugin Json Rest API.
To test the plugin the documentation states that I should just use:
$ curl -i http://testpress-maxximus.rhcloud.com/wp-json/
HTTP/1.1 404 Not Found
Date: Sat, 24 May 2014 07:01:21 GMT
Server: Apache/2.2.15 (Red Hat)
Content-Length: 303
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /wp-json/ was not found on this server.</p>
<hr>
<address>Apache/2.2.15 (Red Hat) Server at testpress-maxximus.rhcloud.com Port 8
0</address>
</body></html>
As you can see nothing is found by the URL. Any recommendations if there is a problem with the API or wordpress?
I appreciate your reply
The current version of REST api for sites with pretty permalinks not enabled, the url
yoursite.com/?rest_route=/
will work .
The WordPress JSON API depends on pretty permalinks, make sure you have them enabled for the site.
In my case, I got this error after installing/configuring apache2 on my local linux machine. I finally found the error to be cause by the rewrite module not being enabled which I fixed using,
sudo a2enmod rewrite
as well as ensuring that my apache2.conf file (located in the folder /etc/apache2) has its<Directory> directive 'AllowOverride' set to all rather than none, from
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
to
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
then I restarted apache2 service and the problem was resolved.
I have faced this issue several times . The solution is this :
Login into your Wordpress site: example.com/wp-admin
Then click on settings
Then click on permalinks
Then set permalinks to "post-name"
Save Changes
Sometimes the solution is crazy and easy! Go to the permalink settings by moving to Admin -> Settings -> Permalinks...then just hit Save Changes without doing anything else! This refreshes the memory of WordPress.
Why is that? For a situation I had before, I had changed the main website URL so I had to refresh the permalinks as well.
I had this same issue and wanted to post my solution in case anyone else comes across this answer and the other answers don't solve the issue, as this happened with me.
In my case I didn't have a .htaccess file with Wordpress' default mod_rewrite rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This solved the issue for me. Per the documentation:
WordPress uses this file to manipulate how Apache serves files from
its root directory, and subdirectories thereof. Most notably, WP
modifies this file to be able to handle pretty permalinks.
I was running WP on a local dev environment in a subdomain of localhost (eg mysite.localhost:8888)
The solution for me was to update the virtual host config in httpd-vhosts.conf to set directory options, similarly to Aurovrata's answer:
<VirtualHost *:8888>
ServerName mysite.localhost
DocumentRoot "/Users/myusername/mysite"
<Directory /Users/myusername/mysite>
Options Indexes FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>
Faced a similar issue, turns out that Apache's mod_rewrite module wasn't enabled. Worked fine after enabling it.
For me, this issue was due to the WP site being developed at the root of a staging URL (ie example.com) but when put live it was installed in a sub-directory (ie example.org/wp)
Before I could make the suggestion from this comment work I had to chmod 664 .htaccess to make it writable by Wordpress. I then resaved the Permalinks as suggested and Wordpress updated the RewriteBase in .htaccess to /wp
If you have correctly installed the plugin, be sure to flush the rewrite rules.
This can be accomplished with the wp-cli: http://wp-cli.org/commands/rewrite/flush/

Server side changes in Angular html5 mode

I was wondering if any one can tell me what are the server side changes which needs to be done when you change your mode in to html5 in Angular js. because When I did tried to change the it to html 5 mode I was unable to go in to my inner html pages. in Angular API it says that suers need to do a server side changes as well.
what are the server side changes
do we need to do any other changes as well ?
You should distinguish two type of calls:
HTML calls from the browser. These should be all remapped to serve your index or whatever.
JSON API requests. These should pass through to your app server.
How to distinguish between these two types of calls, and how to remap the former, depends strongly on your setup.
If you are using nginx, for instance, a combination of checking $http_accept to be application/json (see http://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Requests and http://wiki.nginx.org/HttpCoreModule#.24http_HEADER) and rewrite (http://wiki.nginx.org/HttpRewriteModule#rewrite) you can achieve what you want.
You need to setup your server to rewrite everything to index.html per: https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-configure-your-server-to-work-with-html5mode ...
<VirtualHost *:80>
ServerName my-app
DocumentRoot /path/to/app
<Directory /path/to/app>
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
</Directory>
</VirtualHost>
My app has params passed to a controller (via ui-router) so before html5mode I would goto
www.blah.com/angapp/#/myUIrouterController?param_x=1&param_y=2
Guessing browsers know that /#/ folder path part should serve index.html.
Now that # would be gone with html5mode, the server by default doesn't know to serve the index.html for that folder since the url will just be:
www.blah.com/angapp/myUIrouterController?param_x=1&param_y=2
myUIRouterController isn't a real file so the server would just serve a 404, Hence why I think the rewrites are needed so it knows to send everything to index.html (so above in combo with the <base> tag should work... note: requireBase is optional but heard it helps older browsers like IE9 maybe).