Skip to content

Instantly share code, notes, and snippets.

@nathanialhenniges
Forked from stmoerman/redirect.md
Created January 17, 2019 19:40
Show Gist options
  • Select an option

  • Save nathanialhenniges/af49cc3b4133053430ff322b162cbf11 to your computer and use it in GitHub Desktop.

Select an option

Save nathanialhenniges/af49cc3b4133053430ff322b162cbf11 to your computer and use it in GitHub Desktop.
Apache2 htaccess redirect non-www to www with SSL/HTTPS

Without HSTS (single redirect):

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

With HSTS (double redirect):

RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{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