WP REST API and "Only authenticated users can access the REST API" - wordpress

I have a wordpress : https://example.com
I need to use WP REST API.
In .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]
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
</IfModule>
# END WordPress
I use theBasic-Auth plugin on my wordpress : https://github.com/WP-API/Basic-Auth
Nevertheless I have:
{
"code": "rest_cannot_access",
"message": "Only authenticated users can access the REST API.",
"data": {
"status": 401
}
}
I tested with http and https.
What's wrong?

Solved!
I changed it to the following
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
The HTTP_AUTHORIZATION rule has to come before the other rules, this is because the L flag exists, the L flag means (last - stop processing rules), because of this it would never come to that rule if it was after the original wordpress rules,

I had the same issue and fixed it by verifying whether there is any other Password protection plugin. I was using such a plugin named, Password Protected and disabled it to generate the authentication token.

Related

How do I configure the htaccess for the woocommerce api and the wordpress api at the same time?

I have the following htaccess to use the Wordpress API which works perfectly for me:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
And then I have this one for the Woocommerce api which also works:
<IfModule mod_rewrite.c>
RewriteEngine On
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
RewriteBase /badamsite/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /badamsite/index.php [L]
</IfModule>
The problem is that if I use one the Wordpress API does not work (Error 401 Authorization denied) and if I use the other the Woocommerce API does not work for me (JSON ERROR: Syntax error)
I don't know how to make both things work at the same time, I appreciate any help.

Woocommerce rest api not working 401 unauthorised

I have wordpress 5.4.1 and woocommerce 4.2 and ssl certificate from let's encrypt
I managed to get a response from the API before using query string https://www.store.com/wp-json/wc/v3/products?consumer_key=ck_XXXX&consumer_secret=cs_XXX but suddenly it stopped working giving me 401 unauthorized error. I am 100% sure about the keys.
I am using php with FastCgi and I read that sometimes the server dosn't read the authorization correctly so I tried the following
I added
<IfModule mod_setenvif>
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
</IfModule>
and
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
to my .htaccess file
Also I added
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
to my httpd.conf file after RewriteEngine on
I also installed the basic auth plugin https://github.com/WP-API/Basic-Auth
All of the above solutions failed to get a response from postman or insomnia with the same error 401 unauthorized.
I finally got the answer after 2 days
The best solution is to remove all the above and just add
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]
so my whole htaccess block will look like:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

WordPress .htaccess: have site in subdomain resolve to TLD/root domain too

I have a site that currently sits on example.com/uk/ - that's the URL, and it physically lives in that location (there is also another WP installation in the root on example.com/). I now need example.co.uk to resolve to the site that lives in example.com/uk/. The new URL needs to be up & working for visitors ASAP, but there are dozens of services integrated with the 'old' URL which can't all be switched instantly, hence the need to have both URLs active at once.
I've managed to get all of this working with a CNAME in the DNS and edits in the wp-config.php to accept both the incoming .com/uk & .co.uk, and it works for the home page; however if I go any deeper on the .co.uk, I get server error.
I've narrowed this down to the .htaccess file, but I can't get it work as expected for both domains. The original .htaccess looked like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /uk/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /uk/index.php [L]
</IfModule>
My latest attempt looks like this:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} example
RewriteCond %{REQUEST_URI} !/uk/
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} example
RewriteCond %{REQUEST_URI} /uk/
RewriteRule . /uk/index.php [L]
...but none of the scripts or CSS load (I think they end up in an infinite loop). I suspect part of the issue is the RewriteBase, but that doesn't seem to respond to RewriteConds.
TL;DR I need a WP website to work on both a root URL and a subdirectory URL, but I can't figure out the .htaccess rules to make it happen.
If using Apache 2.4+, this can be done with an <If/Else> conditional in the .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
<If "%{HTTP_HOST} == 'example.co.uk'">
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</If>
<ElseIf "%{HTTP_HOST} == 'example.com'">
RewriteBase /uk/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /uk/index.php [L]
</ElseIf>
</IfModule>
Note that your wp-config.php file also needs to be able to accept & resolve the site to the incoming domain:
$allowableDomains = array(
'example.co.uk',
'example.com/uk',
);
define('DOMAIN_ARRAY', $allowableDomains);
function in_arrayi($needle, $haystack) {
return in_array(strtolower($needle), array_map('strtolower', $haystack));
}
# The protocol should be whatever your site is using:
$protocol = 'https://';
$refHost = $_SERVER['HTTP_HOST'];
if (in_arrayi($refHost, $allowableDomains)) {
$siteURL = $protocol . $refHost;
} else {
$siteURL = $protocol . $allowableDomains[0];
}
define('WP_SITEURL', $siteURL);
define('WP_HOME', $siteURL);
This will allow any incoming domain, but default to .co.uk if you're pointing any other domains that aren't in the $allowableDomains array.

wordpress redirect loop on mod rewrite

I have wordpress setup to change the admin url to www.mydomain.com/admin. But now I have updated the permalinks to use the post name instaed of the id I get a redirect loop when I try to access the admin page. The problem is with the extra rules wordpress asked me to add to the .htaccess but I am unsure how to change it to allow both rules.
Wordpress 3.5.2 rewrite rules as below.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
RewriteRule ^admin/(.*) wp-admin/$1?%{QUERY_STRING} [L]
Anyone know what i need to do to allow both rules?
Was trying out a few solutions and it would seem that having the rewrite rule for changing the admin url to www.mydomain.com/admin BEFORE the mod-rewrite rules is the key to allowing both rules to work together. See updated/fixed .htaccess code below.
RewriteRule ^admin/(.*) wp-admin/$1?%{QUERY_STRING} [L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

htaccess rewriting user pretty url to perform query

Say I have a site mysite.com, which is a wordpress site.
I already have the following rewrite rules to remove index.php:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
if I go to mysite.com/?blog_type=a-blog-type I get the proper page with only the blogs of that type
What I would like, is for the user to be able to type in mysite.com/blogs/a-blog-type/ and have that acutally do the query, but keep on displaying /blogs/a-blog-type/
I've looked at many similar answers on here, but most seem to make it so that it just redirects from the query, to the pretty url, which I don't want, as the pretty url just leads to a 404 error as it's not doing the query.
Thanks for the help.
This rewrite rule will take any mysite.com/blogs/a-blog-type/ URL and rewrite it to mysite.com/?blog_type=a-blog-type. So mysite.com/blogs/a-blog-type/ would be displayed to the user, but mysite.com/?blog_type=a-blog-type will be loaded.
RewriteRule ^blogs/([^/]*)((/)?)$ ?blog_type=$1 [nc,l]
EDIT: So here's how your config will now look:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^blogs/([^/]*)((/)?)$ ?blog_type=$1 [nc,l]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Resources