Skip to content

Instantly share code, notes, and snippets.

@Khodl
Last active July 24, 2017 23:47
Show Gist options
  • Save Khodl/4d2d4e74272c75a1719452d632a4acc4 to your computer and use it in GitHub Desktop.
Save Khodl/4d2d4e74272c75a1719452d632a4acc4 to your computer and use it in GitHub Desktop.
Generic way to force https and redirect to naked domain
RewriteEngine On
# Remove www (do it first in case https is not supported by subdomain)
RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# Force https...
# ... if not local
RewriteCond %{SERVER_NAME} !=127.0.0.1
RewriteCond %{SERVER_NAME} !=localhost
# ... and not activated
RewriteCond %{HTTPS} !=on
# ... and proxy (on Heroku, for instance)
#RewriteCond %{HTTP:X-Forwarded-Proto} .
#RewriteCond %{HTTP:X-Forwarded-Proto} !https
# ... Redirect:
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment