htaccess redirect for non-www both http and https
Solution 1:
Try this rule:
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Here’s an explanation:
- The first condition tests if the HTTP header field Host has the required format (contains exactly one period).
- The second condition tests if the concatenated value of the value of the HTTPS variable (values
on
andoff
) ands
(so eitherons
oroffs
) is equal toons
and captures thes
. This means if%{HTTPS}s
evaluates toons
, the first matching group iss
and empty otherwise. - The rule will match all requests as every string has a start (marked with
^
) and redirects them to the evaluated value ofhttp%1://www.%{HTTP_HOST}%{REQUEST_URI}
if both conditions are true. Where%1
is the first matching group of the previous condition (s
if HTTPS and empty otherwise),%{HTTP_HOST}
is the HTTP Host of the request and%{REQUEST_URI}
is the absolute URL path that was requested.
Solution 2:
Try adding this RewriteCond
to your .htaccess file to catch SSL only - then do the same thing for standard http.
RewriteCond %{HTTPS} on