Skip to content

Instantly share code, notes, and snippets.

@bert-w
Last active May 17, 2019 22:05
Embed
What would you like to do?
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