Skip to content

Instantly share code, notes, and snippets.

Last active May 1, 2023 19:24
Show Gist options
  • Star 44 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save nurtext/b6ac07ac7d8c372bc8eb to your computer and use it in GitHub Desktop.
Save nurtext/b6ac07ac7d8c372bc8eb to your computer and use it in GitHub Desktop.
Tell apache we're on HTTPS if reverse proxy is serving the site using SSL
# Let apache know we're behind a SSL reverse proxy
SetEnvIf X-Forwarded-Proto "https" HTTPS=on
# Redirect to HTTPS
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Copy link

any solution for nginx ?

Copy link

For future Googlers, I found this also useful if you have WordPress running behind AWS ALB and the ALB terminates the TLS certificate (e.g. using ACM).

In this case, the WordPress ECS container, Lightsail instsance or EC2 instance (depending on how you run it), is forwarded traffic on port 80 and WordPress has no information that it's running on HTTPs. The only indication is the fact that the ALB passes the X-Forwarded-Proto=https header, but WordPress doesn't care about that. Its is_ssl() built-in function only cares about $_SERVER['HTTPS']='on'; being set. The first line in this Gist does just that. Sets the latter if the former is present.

Copy link

Copy link

i have a nginx that reverse proxy wordpress by https
the wordpress is running by an docker with apache2 inside
but now it redirect between http and https, how to turn off redirect?

Copy link

is can be a couple of reasons you need to be more specific.
Do you use Nginx? cPanel? Plesk? Engintron on cPanel?

I can tell you this code is effective for people who have NGINX and reverse proxy.
and I don't gonna lie if you have cPanel has the big chance this code is not recommended anymore.

cPanel design himself (APACHE as well) with new features with an interface (like windows not work on DOS anymore and when you compile it you actually need to do it from the interface).

if you work on the cloud, ssh, and individual WordPress standalone is a little different.
if you use cPanel is be easier to help you, btw good to know for you:

if you have cPanel (even from the hosting is no matter)
connect to the link they are able to help you,

if you have something also like Plesk, cPanel Engintron, stuff like this, more than here I will try help you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment