Skip to content

Instantly share code, notes, and snippets.

@lutsen
Last active May 13, 2018 21:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lutsen/7825325 to your computer and use it in GitHub Desktop.
Save lutsen/7825325 to your computer and use it in GitHub Desktop.
RewriteEngine On
# Some hosts may require you to use the `RewriteBase` directive.
# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
# RewriteBase /
# Force www
# - Exclude urls starting with loaclhost
# - Check whether the Host value is not empty (in case of HTTP/1.0)
# - Check for 2 dots or not (instead of checking for www, because that would break subdomains)
# - Checks for HTTPS (%{HTTPS} is either on or off, so %{HTTPS}s is either ons or offs and in case of ons the s is matched)
# - The substitution part of RewriteRule then just merges the information parts to a full URL
RewriteCond %{HTTP_HOST} !^localhost
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ HTTP%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment