How to force https and www. in .htaccess? - .htaccess

comI tried following code in .htaccess to force https and www. Because I allways want a URL like https://www.domain.com
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^.*domain\.com$ [NC]
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301,L]
If i type in these cases into the browser it works fine:
www.domain.com
domain.com
http://www.domain.com
http://domain.com
But if i type following line - no forwarding happens:
https://domain.com
How can i fix this?

You need these 2 rules for all the redirections:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301,L,NE]

Related

redirect www URL to non www with https

How can I redirect all www requests to non-www with https?
For example all following 3 URL`s:
http://www.example.com/about-us
http://example.com/about-us
https://www.example.com/about-us
Should redirect to https://example.com/about-us
I have tried answers posted at
redirection issue in www to non www with ssl/https
and
.htaccess redirect www to non-www with SSL/HTTPS but they are not working. My current rule is
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443 [OR]
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]
Please guide.
Try the following code "
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://example.com/$1 [L,NE,R=302]
So , the scenario above is to catch all http with or without www by this condition RewriteCond %{HTTPS} off and then catch all www with this condition RewriteCond %{HTTP_HOST} ^www\. and then force all into https:// .
Note: clear your browser cache and test it and if it's Ok change 302 to 301 to get permanent redirection.
Update :
go to these lines in your .htaccess :
# RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
# RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]
make them without # like this :
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]
Then go directly to the code in the last of page :
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://phoddalo.com/$1 [L,NE,R=302]
change it to this :
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://phoddalo.com/$1 [L,NE,R=302]
Test it and make sure you clear your browser cache .
If it is ok , change 302 to 301 as i mentioned above .

htaccess redirect https://www.example.net to https://www.example.com/members/

My title says it all. I tried the following, but it only works for the non-https version.
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com/members/ [R=301,L]
Try below rule,
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com/members/ [R=301,L]

http or http www to https www with only one redirection

in .htacces I am trying to redirect to https://www
this is working fine
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
but with 2 redirections I'd like to do it with only 1 redirection
I have tried
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
it works fine but if the s is removed (step 2) I get 2 times www
1) https://www.domain.com --> 2) http://www.domain.com --> 3) https://www.www.domain.com
how can I redirect ?
http://domain.com
http://www.domain.com
https://domain.com
to
https://www.domain.com
with only one redirection and no error whatever happen
thanks for helping
This does only one redirection. Yes, it is still two rules, but only one redirection will be performed:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301,QSA]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301,QSA]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301,QSA]
A more compact alternative, though not really faster or more efficient, would be that:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301,QSA]
The third condition in this always matches, it is only used to extract the part of the host name without a potantial leading www..

htaccess redirect domain to https, subdomain to http and www to non-www

I’m trying to do that:
Force the https for my main domain.
http or https://www.domain.com -> https://domain.com
http or https://domain.com -> https://domain.com
But not for subdomains
http or https://www.subdomain.domain.com -> http://subdomain.domain.com
http or https://subdomain.domain.com -> http://subdomain.domain.com
And always removing the www.
Now i have that:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This redirects www to non-www and http to https but not for subdomains. The subdomain remains with www and the https.
Thanks
You can use these 2 rules:
# for main domain
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://domain.com%{REQUEST_URI} [R=301,L,NE]
# for sub domain
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain\.com$ [NC]
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ http://subdomain.domain.com%{REQUEST_URI} [R=301,L,NE]
In order to get the combination you want. You will need to use your domains.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?sub\.example\.com [NC]
RewriteCond %{HTTPS} on
RewriteRule (.*) http://sub.example.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} !^sub\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^example\.com$ [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
I also used the following and it worked great. I was having a hard time pointing my primary domains www.domain.com and http://domain.com to https://domain.com
Thanks very much; as I've been trying to get this done for hours now!!!
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://domain.com%{REQUEST_URI} [R=301,L,NE]

is my .htaccess correct for non-www to www AND to force https?

We are switching to HTTPS.
Our current .htaccess file has the following code to force non-www to www:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Now, for HTTPS this is my proposed addition:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
My question is - how do I merge - or combine - the non-www to www AND HTTPS? Or would I just keep the order as is above?
Thanks.
PS I have researched this but I don't seem to find any concise answer....
You can use that:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,R=301,L]
Or without domain name:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
I recommend you use the first. If indicate the domain name is not a problem.

Resources