Era Host hosting
EraHost – Free Domain, Cheap Hosting!
Client Area
Support 24/7
Menu

Ensuring Apache Passes Authorization Headers for apache_request_headers()

4 min read
07.09.2025

Understanding the Problem

  • By default, some Apache configurations may strip the Authorization header or other headers before passing the request to PHP.
  • This happens due to either:
    • Missing or incorrect Apache settings.
    • Security restrictions in the server environment.

To solve this, we need to configure Apache properly.

Apache Authorization Headers PHP
PHP-FPM strips Authorization — CGIPassAuth or .htaccess rewrite.

For closely related apache_request_headers / Authorization topics, see Verifying Apache Configuration for apache_request_headers, Programmer's Guide to apache_request_headers, and Non-Technical User Fix Guide.

Check If Apache Headers Module Is Enabled

The mod_headers module is required to pass and manipulate HTTP headers.

How to Check:

  1. Open a terminal and run:
    apachectl -M | grep headers
    • If you see headers_module (shared) in the output, the module is enabled.
    • If not, enable it.

Enable mod_headers:

  1. Run the following command to enable mod_headers:
    sudo a2enmod headers
  2. Restart Apache to apply the changes:
    sudo systemctl restart apache2
Linux VDS
High performance for your projects
  • Root access and flexible setup
  • Control panel
  • NVMe disks
  • DDR5
Linux VDS

Preserve Authorization Header in Apache

To ensure Apache passes the Authorization header to PHP:

Edit the Apache Configuration

  1. Open the main Apache configuration file:
    sudo nano /etc/apache2/apache2.conf

    Or edit the specific virtual host configuration file:

    sudo nano /etc/apache2/sites-available/your-site.conf
  2. Add the following directive inside the <VirtualHost> block or globally:
    SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
  3. Save the file and exit.
  4. Restart Apache to apply the changes:
    sudo systemctl restart apache2

Update .htaccess File (If Used)

If your application uses .htaccess, you can also add the rule to preserve the Authorization header there.

Steps:

  1. Open the .htaccess file in your project's root directory.
  2. Add the following lines:
    RewriteEngine On
    RewriteCond %{HTTP:Authorization} ^(.*)
    RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
  3. Save the file and reload your website.

Debug Headers in PHP

To confirm that the Authorization header is being passed correctly, use a simple PHP script:

Debugging Script:

Create a file called debug_headers.php with the following content:

<?php
// Try to retrieve headers using apache_request_headers
$headers = apache_request_headers();
if ($headers) {
    echo "Headers:\n";
    print_r($headers);
} else {
    echo "apache_request_headers() is not working.\n";
}

// Fallback to $_SERVER for debugging
echo "\nHTTP_AUTHORIZATION:\n";
echo $_SERVER['HTTP_AUTHORIZATION'] ?? 'Not Found';
?>

Test Using cURL

To test if Apache is passing the Authorization header, use the following curl command:

curl -H "Authorization: Bearer YOUR_TOKEN" http://yourwebsite.com/debug_headers.php

Check the output of the script:

  • If the Authorization header appears in the response, the issue is resolved.
  • If not, proceed to the next step.

Troubleshooting Common Issues

Authorization Header Missing After Reverse Proxy

If your server uses a reverse proxy (e.g., NGINX or AWS Load Balancer), the proxy might strip the Authorization header before passing it to Apache.

Fix for NGINX:

  1. Open the NGINX configuration file:
    sudo nano /etc/nginx/nginx.conf

    Or edit the site-specific configuration file.

  2. Add the following directive inside the relevant location block:
    proxy_set_header Authorization $http_authorization;
  3. Reload NGINX:
    sudo systemctl reload nginx

Clear Environment Variables in PHP-FPM

If you're using PHP-FPM, it may clear environment variables, including Authorization.

Steps to Fix:

  1. Open the PHP-FPM pool configuration file:
    sudo nano /etc/php/8.0/fpm/pool.d/www.conf

    Replace 8.0 with your PHP version.

  2. Look for the clear_env directive and set it to no:
    clear_env = no
  3. Restart PHP-FPM:
    sudo systemctl restart php8.0-fpm

Final Testing

After making all changes:

  1. Restart Apache:
    sudo systemctl restart apache2
  2. Test again using the debugging script and curl to confirm the Authorization header is being passed to PHP.

Summary

To ensure Apache passes the Authorization header and supports apache_request_headers():

  1. Enable the mod_headers module.
  2. Use the SetEnvIf directive in the Apache configuration to preserve headers.
  3. Update the .htaccess file if your application uses it.
  4. Fix proxy configurations if a reverse proxy is in use.
  5. Test the solution using a debugging PHP script and tools like curl.

By following these steps, you can ensure apache_request_headers() works as expected, passing critical headers like Authorization to your PHP application.

Frequently asked questions
`curl -H 'Authorization: Bearer test123' https://yoursite.com/test.php` where test.php is ``. If Authorization key appears in the dump, it's passing through. If not, Apache or PHP-FPM is stripping it. CGIPassAuth On in vhost or .htaccess is the most common fix.
Apache 2.4.13+ supports `CGIPassAuth on` in .htaccess. Older Apache: must be in vhost. Also requires `AllowOverride All` on the parent vhost — many shared hosts don't allow it. If your shared host doesn't allow CGIPassAuth and you can't change vhost, fall back to the SetEnvIf rewrite trick: `SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1`.
Exactly the same. WP REST API expects `apache_request_headers()` to return Authorization; with PHP-FPM it doesn't unless CGIPassAuth or rewrite is in place. WP's `wp-includes/class-wp-rest-server.php` falls back to `$_SERVER['HTTP_AUTHORIZATION']` if available — the rewrite trick populates that. Without either, REST auth silently fails.
Different stack, similar issue. Nginx must forward `fastcgi_param HTTP_AUTHORIZATION $http_authorization;` to PHP-FPM. Without it, PHP doesn't see Authorization. Add to your nginx site config's PHP location block, reload. The fix philosophy is the same (explicit pass-through) but the syntax is different from Apache.
Related articles
Fixing apache_request_headers Authorization Not Working — A System Administrator's Guide
Cache-Control: max-age with public
Debugging session_start() Errors in PHP Using Error Logs