Skip to content

Instantly share code, notes, and snippets.

@Sija
Created July 26, 2016 00:47
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 Sija/fe434bb19026e972ae353c34e93c4582 to your computer and use it in GitHub Desktop.
Save Sija/fe434bb19026e972ae353c34e93c4582 to your computer and use it in GitHub Desktop.
no-www mod_rewrite apache .htaccess redirect
<IfModule mod_rewrite.c>
RewriteEngine on
# Preserve HTTP/HTTPS protocol in .htaccess redirects
# http://stackoverflow.com/a/20419821/601064
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ - [env=proto:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ - [env=proto:http]
# Rewrite "www.example.com -> example.com"
# http://stackoverflow.com/a/2361618/601064
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ %{ENV:proto}://%1%{REQUEST_URI} [R=301,L]
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment