Woocommerce rest api not working 401 unauthorised - woocommerce

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>

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.

Wordpress HTTP Request 501 Internal Server Error

Hello i have digitalocean one click application contains Wordpress.
I add domain to my server and create SSL certificate for my server.
https://example.com works good but when i request
http://example.com its redirect -> http://myserverip.com and gives that error:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at webmaster#localhost to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
Additionally, a 301 Moved Permanently error was encountered while trying to use an ErrorDocument to handle the request.
Please help me about it. I try to redirect http to https but its not work!
Here it is my .htaccess file contains:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# 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
Try this, worked for me:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTPS} =off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,L,R=301]
</IfModule>

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

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.

How to solve issues regarding custom 500 and 503 error pages

My Website's
Header:
HTTP/1.1
Server:
Apache
CMS:
WordPress
In .htaccess file I'd the following:
# 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
Now, I've created a custompage.html page and kept it in the website's root (so that it can be accessed as mywebsite.com/custompage.html). Then I added the following code to .htaccess file:
# 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]
ErrorDocument 503 /custompage.html
ErrorDocument 500 /custompage.html
</IfModule>
# END WordPress
But, still it seems to be not working during a 500 or 503. What should I do to make my custom pages to work? - Please help
Why don't you try using the whole URL, to make sure that there is not any problem there.
ErrorDocument 500 http://error.example.com/server_error.html

I got a stuck with .htaccess and rewrite

I'm using Lamp server on linuxmint and i installed 2 wordpress platforms on it one called "bfsite" and the other one called "titus". I modified the .htacces file as WordPress suggest me to do
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /bfsite/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /bfsite/index.php [L]
</IfModule>
This one is for bfsite, and the I modify for the /titus platform
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /titus/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /titus/index.php [L]
</IfModule>
And a simple redirect.
Redirect /index.php http://89.39.166.81/titus
As you see I made my locahost online for you to see what's happening, when I try to go on
http://89.39.166.81/titus/about
It returns to me with http://localhost/titus/dev/ and also if you force to with this path
http://89.39.166.81/titus/dev/
It comes with a 404 and page not found error, but is going with the "bfsite" wordpress platform.
since you are under a ubuntu distro you can use this
sudo a2enmod rewrite
-- then you can enable postname from wp backend. good luck!

Resources