Removing X-Powered-By

Removing X-Powered-By

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Removing X-Powered-By in Legacy PHP Environments

The X-Powered-By HTTP header is a default behavior in many web server configurations, but it is widely regarded as a security risk. By explicitly announcing the server software and the version of the scripting engine running behind it, you provide attackers with specific information needed to target known vulnerabilities. While modern PHP versions allow easy suppression via header_remove() within .htaccess, legacy environments on Apache—specifically those running PHP 5.21 or earlier—lack this functionality. When standard application-level fixes fail in production, the solution often lies in adjusting the server configuration directly rather than relying on directives that the web server ignores.

In a typical setup involving Apache and older PHP versions, attempting to use php_value header_remove "X-Powered-By" inside an .htaccess file will result in a silent failure. This happens because the header_remove directive was not introduced until PHP 5.3.0. When you encounter this behavior, where the change works locally but fails on your production Apache server, it indicates that the server is likely serving files directly or that the specific PHP handler configuration does not support runtime header manipulation for that particular response.

To effectively remove the header in these constrained environments, you must bypass the PHP application logic entirely and handle the request at the Apache level using mod_headers. The most reliable method involves editing the main Apache configuration file, often located at /etc/apache2/mods-available/headers.conf on Debian-based systems or /etc/httpd/conf.d/headers.conf on Red Hat distributions.

You need to ensure that the mod_headers module is loaded. If it is not already active, you must enable it first:

LoadModule headers_module modules/mod_headers.so

Once the module is loaded, you can add a directive within your virtual host configuration or a specific .conf file in the conf.d directory to strip the header for all requests:

<IfModule mod_headers.c>
    Header unset X-Powered-By
</IfModule>

Placing this block inside a <VirtualHost> container ensures the rule applies only to that specific site, preventing unintended side effects on other domains hosted on the same machine. This approach is superior to trying to force PHP directives in older versions because it operates independently of the PHP interpreter's capabilities. It intercepts the response before it reaches the client, effectively removing the header regardless of the PHP version running the script.

However, there are nuances to consider regarding where this configuration lives. If your application runs under a specific user and you do not have root access to modify the main Apache config files, you may need to create a custom .conf file in the conf.d directory and restart the service for changes to take effect.

sudo nano /etc/apache2/conf.d/strip-headers.conf
# Or on RHEL/CentOS:
sudo nano /etc/httpd/conf.d/strip-headers.conf

After defining the rules, a restart of the Apache service is mandatory for the new headers directives to be read. Attempting to edit an .htaccess file in this specific legacy scenario often yields no results because mod_headers may not follow standard overrides correctly if the AllowOverride directive does not explicitly permit header modifications, or if the PHP handler takes precedence in a way that bypasses Apache-level header stripping for dynamic content.

If you are maintaining a codebase that requires strict security hygiene but cannot upgrade your PHP runtime to a version supporting native header removal, integrating this server-side fix is essential. For developers using modern stacks like Laravel, this manual configuration step is usually unnecessary as newer versions handle header sanitization automatically or via built-in middleware, making the transition from legacy PHP much smoother.

Ultimately, while header_remove() simplifies the task in modern PHP, understanding the underlying Apache mechanics allows you to secure legacy systems without waiting for an environment upgrade. Always test these configuration changes on a staging server before applying them to production to verify that dynamic content continues to render correctly without the exposed header.

Our network