jeni
jeni

Reputation: 41

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 entire site is now in https version.

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

How do I do that with an .htaccess file?

Upvotes: 4

Views: 12394

Answers (4)

John Robey
John Robey

Reputation: 1

How about this as https uses port 443

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

Upvotes: 0

George Guo
George Guo

Reputation: 13

I might have the solution:

RewriteEngine  on

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

Replace [domain] and the extension to your own domain. Tell me if this doesn't work.

Upvotes: -1

Igor Grinkin
Igor Grinkin

Reputation: 61

Non-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]

Upvotes: 6

Lawrence Woodman
Lawrence Woodman

Reputation: 1444

Put the following lines in to your .htaccess file and replace mydomain.com with the domain that 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]

For this to work you will have to remove the existing entries that you mention in your question from the .htaccess file

Upvotes: -1

Related Questions