Reputation: 2274
I'm currently using this to force https and www via htaccess:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=302]
Now I want to change the R=302
in R=301
, and ditch the domain name itself. The reason why I don't want example.com to be in there is because it might be that for some countries the domain name will be different in the future.
So I was thinking
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
... but that would redirect www.example.com to www.www.example.com right? How to do this properly in this specific case? I still want to force www and https in one single redirect.
Upvotes: 1
Views: 1454
Reputation: 785146
I still want to force www and https in one single redirect.
You need to use this redirect rule like this to avoid hardcoding:
# add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
Upvotes: 1