Skip to content

Instantly share code, notes, and snippets.

@bert-w
Last active May 17, 2019 22:05
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 bert-w/3ee1089f33dd5f7022e8fff52df307b7 to your computer and use it in GitHub Desktop.
Save bert-w/3ee1089f33dd5f7022e8fff52df307b7 to your computer and use it in GitHub Desktop.
Generic htaccess rewrite to HTTPS
# Rewrite non-https to https + www
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
</IfModule>
# Rewrite non-https to https without www
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
</IfModule>
# Rewrite non-https subdomain to https subdomain
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off [NC]
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment