sandeep krishna
sandeep krishna

Reputation: 475

force redirection from https://example.com to https://www.example.com

I know this is a question that is asked several time. I tried several rules but redirection of https://example.com to https://www.example.com is not working.

My current redirection rule in the Apache vHost of non SSL is pasted below


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

The above rule works fine for http://example.com and http://www.example.com

Upvotes: 0

Views: 27

Answers (2)

MrWhite
MrWhite

Reputation: 45914

My current redirection rule in the apache vHost of non SSL is pasted below

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

You already have a solution, but as you have found, the above redirect will obviously only apply to HTTP requests when in the "vhost of non ssl". In this case, the HTTPS server variable is always "off" - so the first RewriteCond directive is entirely redundant.

However, you don't need mod_rewrite at all when redirecting from HTTP to HTTPS in the HTTP-virtualhost. A simple mod_alias Redirect will do the job much "better":

Redirect 301 / https://www.example.com/

Upvotes: 1

sandeep krishna
sandeep krishna

Reputation: 475

I find it. This needs to be added in the ssl vhost file.

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

Upvotes: 1

Related Questions