Last active
May 13, 2018 21:19
-
-
Save lutsen/7825325 to your computer and use it in GitHub Desktop.
Force “www.” in a generic way.
(From http://stackoverflow.com/questions/4916222/htaccess-how-to-force-www-in-a-generic-way)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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