Skip to content

Instantly share code, notes, and snippets.

@sidisinsane
Last active August 29, 2015 14:16
Show Gist options
  • Save sidisinsane/7c7ee71800169c8e791b to your computer and use it in GitHub Desktop.
Save sidisinsane/7c7ee71800169c8e791b to your computer and use it in GitHub Desktop.
generic force www (incl. subdomains) and non-www
########################################################################################
# store requested protocol in %{ENV:protocol}
########################################################################################
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ - [env=protocol:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ - [env=protocol:http]
########################################################################################
# force non www
########################################################################################
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ %{ENV:protocol}://%1/$1 [R=301,L]
########################################################################################
# force www (including subdomains)
########################################################################################
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} (.*)
RewriteRule (.*) %{ENV:protocol}://www.%1/$1 [R=301,L]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment