WordPress behind a reverse proxy (with Polylang)

WordPress require some management when is behind a reverse proxy and depends a lot on how much this reverse proxy changes “things”.
Every service is different (Apache, Nginx, FrontDoor, CloudFlare, etc), if the folder is in a subfolder or a subdomain, if require a ssl certificate but the final page will use a different one and so on.

An example with Apache

In this case we will use, as example, a website in a subfolder on the final domain but in reality is a subdomain (so we can generate a dedicated ssl certificate, because the reverse proxy require that the original domain have one for validation, in this example).

This is an example of the VirtualHosts for Apache, this is something that you can’t configure in CPanel but only on your own VPS.

<IfModule mod_ssl.c>
<VirtualHost *:443>
    DocumentRoot "/var/www/your-folder"
    Alias /your-folder/ "/var/www/your-folder"/"
    ServerName your-definitive-domain.com
    ServerAlias www.your-definitive-domain.com
    ServerAlias your-subdomain.domain.com
    ServerAdmin webmaster@localhost
    <Directory "/var/www/your-folder"">
        allow from all
        Order allow,deny
        AllowOverride All
        Require all granted
    SSLEngine off
#LogLevel debug
SSLCertificateFile /etc/letsencrypt/live/your-subdomain.domain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/your-subdomain.domain.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf

In this config, it is define the folder path, the various domains who will respond it. Also, we have a ssl certificate, to generate it set (only) as servername your-subdomain.domain.com, execute certbot (to get a Let’s Encrypt certificate) and later set the servername as in the snippet.

There is also the parameter ready to enable the full log to apache in case you need it.

Reload apache and now the domain will respond rightly to your-definitive-domain.com/your-folder, now you need to set the domain in the reverse proxy of your choice.

Few PHP lines for your wp-config.php

Another step is a tiny code in your wp-config.php (this file is not changed by the WordPress updates).
WordPress basically will work with a reverse proxy setting the your-definitive-domain.com as domain, but with permalinks enabled and with plugins that manipulate the URLs this is not enough.

Like for this case with Polylang (a multilanguage plugin, that change the URL based on the browser language, subdomain support per locale like a multisite etc) it is required another step.
As PHP side is not able to identify that the request is from the reverse proxy, will map the URL internally as your-subdomain.domain.com instead of your-definitive-domain.com.

So we need to set some global PHP variables to the right domain manually.

if ( isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] == 'your-subdomain.domain.com' ) {
  $_SERVER['HTTP_HOST'] = 'your-definitive-domain.com';

$_SERVER['SERVER_NAME'] = 'your-definitive-domain.com';

In this way the web server is managing everything rightly, also WordPress will use the right domain from the PHP data and the DB data on the installation and they will match.

Without that snippet is not able to identify the host and will go in a redirect loop.

Liked it? Take a second to support Mte90 on Patreon!
Become a patron at Patreon!

Leave a Reply

Your email address will not be published. Required fields are marked *