☰
WEB SECURITY / PRACTICE

How to Protect WordPress from Host Header Injection

How to Protect WordPress from Host Header Injection

The Host header tells the web server which domain an HTTP request is intended for. If the server accepts any Host value, an attacker can send a request containing another domain and make the site use it when generating URLs.

Possible consequences include incorrect redirects, password-reset links containing an attacker-controlled domain, invalid URLs in emails, caching problems, and potential phishing scenarios. Protection must be configured at the web server, reverse proxy, or CDN level—not only in WordPress.

What Is Host Header Injection?

In a typical request, the browser sends the server a value such as: Host: example.com. The web server uses it to select a virtual host, while the application may use it to build absolute URLs.

The problem occurs when the server or proxy accepts an arbitrary Host value and passes it on to WordPress. For example, a request with Host: attacker.example may reach your site. If a plugin or custom code builds URLs based on HTTP_HOST, an attacker-controlled domain may appear in an email or redirect.

The Host header is not an authentication mechanism by itself. Its value comes from the client, so it must not be used without validation to generate links, determine the site address, or make security-related decisions.

  • domain substitution in HTTP and HTTPS redirects;
  • incorrect links in password-reset emails;
  • cache poisoning if the cache handles Host incorrectly;
  • phishing URL generation by system or third-party plugins;
  • unexpected behavior in a reverse proxy, CDN, or load balancer.

Define the Official Domains and Canonical Address

Start by making an exact list of the domains that should actually serve the site. This may include the primary domain, a www variant, and, if necessary, separate permitted domains for regional versions. Do not leave a rule that accepts any domain unless there is a justified reason to do so.

Choose one canonical address, such as https://example.com, and define how the permitted alternative domain should behave. Usually, the alternative domain should receive a permanent redirect to the canonical one. An unknown Host should preferably be rejected rather than redirected, because a redirect may expose the unsafe header value in the Location header.

In WordPress settings, the site address and WordPress address must match the intended domain and protocol. Before changing them, verify that you have a backup and access to the file system or database: an incorrect address can make it harder to access the administration area.

  • set the HTTPS domain as the primary domain;
  • do not add test or old domains to the allowlist unless necessary;
  • document separately whether the www version should work;
  • make sure DNS for old domains does not point to this server without a clear reason.

Configure Host Validation in Nginx

For Nginx, a practical approach is to create a default server that rejects unknown requests and separate server blocks only for permitted domains. Rejection should happen before the request is passed to PHP-FPM, so WordPress and its plugins do not process an unwanted Host at all.

The following example is illustrative. Adapt it to your configuration structure, certificates, IPv6 setup, CDN, and PHP deployment method. Do not paste the code into a production server without checking the syntax and testing it.

  • create a separate default_server for HTTP and HTTPS;
  • specify exact names with server_name in the permitted server blocks;
  • do not pass unknown requests to a location block containing fastcgi_pass;
  • after making changes, check the configuration using the command required by your system before reloading Nginx.
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    return 444;
}

server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;
    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;
    server_name _;
    ssl_reject_handshake on;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name example.com;
    # ssl_certificate and ssl_certificate_key are configured separately
    # The standard WordPress configuration follows
}

Should You Use Response Code 444?

444 is a non-standard Nginx code that closes the connection without sending a normal HTTP response. It can reduce unnecessary noise in logs, but it is not a universal standard and is not supported by every proxy.

If your infrastructure includes a CDN, load balancer, or another web server, it may be more practical to use 400 Bad Request or another policy that your platform handles correctly. The key point is to reject the unknown Host before PHP starts.

Check Apache, the Reverse Proxy, and the CDN

Protection on the origin server may not work as expected if a CDN, load balancer, or reverse proxy is placed in front of it. The client Host may be changed, duplicated, or passed in X-Forwarded-Host. Clearly define which component is the trusted source of information about the external domain.

On Apache, check the VirtualHost order: the first virtual host for an address and port may receive requests for unknown domains. Create a separate default virtual host that rejects such requests, and restrict permitted domains in the production VirtualHost. The exact directives depend on the Apache version, TLS design, and module configuration.

In the CDN, enable permitted-domain validation if the feature is available, and do not allow arbitrary Host values to be forwarded to the origin. Also check cache rules: the domain, protocol, and important headers must not cause responses from different sites to be mixed.

  • determine where TLS terminates;
  • agree on which Host value is sent to the origin;
  • process X-Forwarded-Host only when it comes from known proxies;
  • do not trust headers that the client can add independently;
  • check IPv4 and IPv6 separately if both are used.

Do Not Build URLs from HTTP_HOST in WordPress

In custom code and templates, do not use HTTP_HOST as the source of the domain for links, redirects, emails, or callback URLs. This value comes from the request and may be forged if the preceding infrastructure layer has not validated it.

For URLs, use the configured WordPress site address and the standard link-generation functions. In third-party code, check locations that directly read HTTP_HOST, SERVER_NAME, X-Forwarded-Host, or similar variables.

Pay particular attention to plugins that send system emails, create links for confirming actions, generate URLs after login, or perform redirects after form submission. Even if WordPress core works correctly, unsafe plugin logic can reopen the issue.

  • do not concatenate HTTP_HOST with a path to create an absolute URL;
  • do not use Host to select a domain in access-control logic;
  • check custom mu-plugins, snippets, and theme code;
  • keep plugins updated and remove unnecessary components;
  • for redirects, allow only expected internal addresses.

How to Test the Protection After Making Changes

Test not only the home page but also every function that creates absolute links. Run checks from an environment where you can control request headers, but do not use real password-reset tokens in public services.

For HTTP, check the primary domain, the permitted alternative domain, and an arbitrary Host. For HTTPS, remember that the certificate and SNI can affect the result, so test the request with the correct domain-to-IP mapping.

  • a request with an unknown Host should receive 400, 444, or another planned rejection;
  • a permitted alternative domain should redirect only to your canonical domain;
  • the Location header must not contain an arbitrary Host from the request;
  • the password-reset form should send links to your domain;
  • contact-form emails and system messages should contain HTTPS URLs for the site;
  • check redirects after login, logout, errors, and form submissions.
curl -I -H 'Host: unknown.example' http://SERVER_IP/
curl -I -H 'Host: example.com' http://SERVER_IP/
curl -I --resolve example.com:443:SERVER_IP https://example.com/

What to Check in the Logs

After deployment, review the logs of Nginx or Apache, the reverse proxy, and the CDN. Look for unfamiliar Host and X-Forwarded-Host values, frequent login requests, and attempts to trigger redirects with unexpected parameters.

Make sure rejected requests do not reach PHP-FPM or create WordPress errors. At the same time, do not record unnecessary personal data or tokens from URLs in the logs.

Common Configuration Mistakes

The most common mistake is adding a redirect for any Host to HTTPS or to a canonical path. This does not solve the problem if the redirect preserves the domain from the request. First validate the Host, and only then redirect permitted addresses.

Another mistake is protecting only Nginx on the origin while all traffic arrives through a CDN. In this setup, rules must be coordinated at every level; otherwise, the CDN may accept an unknown domain or pass it to the server.

It is also unsafe to blindly rewrite HTTP_HOST in PHP or trust every X-Forwarded-Host value. This may hide the symptom without fixing the incorrect configuration and can sometimes break legitimate proxy requests.

  • do not test the protection only through a browser;
  • do not consider HTTPS alone sufficient protection;
  • do not add every domain found in the logs to the allowlist;
  • do not change the WordPress address without a backup and a rollback plan;
  • do not forget cron, CLI, staging, and separate subdomains.

Frequently asked questions

Are correct Site URL settings in WordPress enough?

No. They reduce the risk of incorrect URLs within WordPress, but they do not prevent the web server from accepting an arbitrary Host. Allowed-domain validation should be performed as early as possible, before PHP starts.

Should all requests with an unknown Host be blocked?

For a typical site, usually yes. If the server hosts multiple sites or services, first create a complete list of legitimate domains and configure separate rules for each of them.

Is the X-Forwarded-Host header dangerous?

It is dangerous when the application trusts it without validating its source. Use this header only from specific trusted proxies that remove incoming values and pass on a consistent domain.

Why did HTTPS stop working after the configuration change?

Possible causes include an incorrect default server, a domain missing from the TLS configuration, an SNI issue, a CDN problem, or an incorrectly specified certificate. Check the configuration step by step, and do not remove the working version without a quick rollback option.

Conclusion

Protection against Host header injection consists of several layers: permitted domains on the web server, correct reverse-proxy and CDN behavior, one canonical HTTPS domain, and safe URL generation in WordPress.

After making changes, always test unknown Host values, redirects, password recovery, system emails, forms, and logs. The best result comes from a configuration in which an unwanted request is rejected before PHP and plugins can see it.

← All web security materials