Skip to content

Instantly share code, notes, and snippets.

@briandoll
Created February 16, 2010 23:58
Show Gist options
  • Save briandoll/306100 to your computer and use it in GitHub Desktop.
Save briandoll/306100 to your computer and use it in GitHub Desktop.
# within your vhost...
RewriteEngine On
# create a variable called 'ps' which stores the original scheme
RewriteCond %{HTTPS} =on
RewriteRule ^(.+)$ - [env=ps:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.+)$ - [env=ps:http]
# if the hostname (in this case, including the subdomain) does not match our desired hostname
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
# redirect it to the correct hostname, using the original scheme and the rest of the URL
RewriteRule ^(.*)$ %{ENV:ps}://www.example.com$1 [R=301,L]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment