skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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]
    
    

  2. 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/
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search