Insights

401 Response when PHP-FPM Enabled on WordPress CMS

We recently migrated our website to another hosting service running FPM/FastCGI
. We noticed under the website CMS -> tools -> Site Health Status
, that all our REST API calls were failing authentication.

It appeared to turn the request into an request from an Anonymous user even though we passed Basic Authentication header.

The response returned was either Gateway timeout OR the response took too long to load, hence many of the plugins like Elementor failed to load , as they internally use the REST API to update the block.

Why did this happen?
Not to worry! This is often easily solvable by a minor .htaccess modification.
This is usually caused by the PHP configuration. This happens because the server is likely configured with PHP in CGI or FastCGI modes. In this mode, by default your web server thinks it’s meant to handle HTTP Auth and then just pass the request on to PHP if it meets the requirements. But we need PHP to get the raw Auth header! So in this case, we’re stashing it in the REMOTE_USER parameter.

Basic Authorization Header Missing

What is the solution
By default, WordPress add a piece of code in the .htaccess file that looks something like below
# 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

What we need to do is add this line directly after the RewriteEngine On
RewriteRule .* – [E=REMOTE_USER:%{HTTP:Authorization}]

The final code piece will look like this
# BEGIN WordPress
<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>

# END WordPress

Once updated, the error should have gone and the affected plugins using the RESP APIs will start working.

Turn insight into action

Need help with apache or a related project?

If this article sparked an idea, question, or project direction, I can help you turn it into a practical next step.

Start a conversationChat on WhatsApp

Related reading

Articles connected to this topic

javascript

Custom Validation for Ninja Form – wordpress

Suppose you have used Ninja Form Plugin in your wordprss website. Ninja form provide you the option to validate the fields like email address, phone numbers format and required validation. But in case you have to add any custom validation then Ninja form provide you the hook to implement custom client side validation.

Jan 10, 2025

Read article

css

How to find selected and unselected item in select2 dropdown

While using select2 library, if you have to find the selected and unselected item based on the change event when you check or uncheck the items. Based on the checked status, you may have to create some custom trigger or events then you can use the below code reference from codepen to achieve the save. [&hellip;]

Dec 1, 2024

Read article

Most recent

Latest posts from the blog

View all articles →