Skip to content

Instantly share code, notes, and snippets.

@joemaller
Last active March 29, 2018 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joemaller/3cfee1583c3d7ca7069e019787ad93cd to your computer and use it in GitHub Desktop.
Save joemaller/3cfee1583c3d7ca7069e019787ad93cd to your computer and use it in GitHub Desktop.
Redirect HTTP to HTTPS from .htaccess
# .htaccess redirects can vary depending on server caching, reverse proxies and CDNs
# Try these RewriteConditions in order, starting from the top and stepping down
# until one works. Only enable one RewriteConds at a time.
RewriteEngine On
# 1. Try this first
RewriteCond %{HTTPS} off
# 2. Docs suggest on|off should work, but try this anyway
# RewriteCond %{HTTPS} !on
# 3. Try port sniffing instead?
# RewriteCond %{SERVER_PORT} !=443
# RewriteCond %{SERVER_PORT} 80
# 4. If the above doesn't work, this might (why?)
# Known to work on LiquidWeb's Cloud sites
# RewriteCond %{ENV:HTTPS} !on
# 5. Cloudflare and maybe other CDNs
# RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment