Skip to content

Instantly share code, notes, and snippets.

@stmoerman
Last active January 17, 2019 19:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stmoerman/da9eab61227e860368e1178b61d43b85 to your computer and use it in GitHub Desktop.
Save stmoerman/da9eab61227e860368e1178b61d43b85 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