How to redirect https://example.com/ to https://www.example.com/

I have this code in my .htaccess file as shown below.

 RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} 

This was added to redirect http to https, so the whole site is now in the https version.

Now I want to redirect from non-www to www. When I type example.com, I have to be redirected to https://www.example.com/

How to do this with a .htaccess file?

+4
source share
4 answers

No SSL redirection:

 RewriteEngine on RewriteCond %{HTTP_HOST} ^domain\.com RewriteRule ^(.*)$ http://www.domain.com$1 [R=permanent,L] 

SSL redirection:

 RewriteEngine on RewriteCond %{HTTP_HOST} ^domain\.com RewriteRule ^(.*)$ https://www.domain.com$1 [R=permanent,L] 
+6
source

How about this since https uses port 443

 RewriteCond %{SERVER_PORT} 443 RewriteRule ^(.*)$ https://www.example.com/$1 [R,L] 
0
source

Put the following lines in your .htaccess file and replace mydomain.com with the domain you want to redirect.

 RewriteEngine On RewriteCond %{HTTP_HOST} ^(www\.){0,1}mydomain.com$ [OR] RewriteCond %{HTTPS_HOST} ^(www\.){0,1}mydomain.com$ RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L] 

To do this, you will have to delete the existing entries that you mentioned in your question from the .htaccess file

-1
source

I may have a solution:

 RewriteEngine on RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^ https://www.[domain].com%{REQUEST_URI} [R=301,L,NE] 

Replace [domain] and extension with your own domain. Tell me if this does not work.

-1
source

Source: https://habr.com/ru/post/1341760/


All Articles