Reputation: 40653
I work on my localhost so I need this exception in my mod_rewrite rule. Currently, I am able to force the "www" when not on localhost using the following:
# Force www, if not in localhost
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Now, I want to also force https (my entire site will be under https). How do I add this to my htaccess?
I tried making my htaccess like this, but this is forcing https on my localhost also:
# Force https, if not in localhost
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Force www, if not in localhost
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
UPDATE 1:
I think I need to remove the first [R=301,L]
.
UPDATE 2:
If I have a URL like this: https://scripts.domain.com
, I do not want it to become: https://www.scripts.domain.com
.
Upvotes: 7
Views: 4990
Reputation: 143946
Add this line above your last rule:
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]{3}$
Upvotes: 4