-1

How to redirect from http to https:

1. redirect http://example.com to https://www.example.com
2. redirect http://www.example.com to https://www.example.com
3. redirect https://example.com to https://www.example.com

Like facebook, google?

Please help me with code in .htaccess or another.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195

3 Answers3

0

To handle all these 3 redirects in a single rule you can use this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"' [OR]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,R=301,L]

RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    When i update this rewriterule it redirect successfully to https but shows error "server not found" and some time show error "The page isn't redirecting properly", but when i remove this error and enter my domain name with https:// it working fine – Rajeev Ranjan Sharma Mar 09 '15 at 15:50
  • 1
    Yeah i have placed correct host name, I am using cloudflare flexible ssl. – Rajeev Ranjan Sharma Mar 10 '15 at 11:48
  • 1
    For all this link `http://example.com http://www.example.com https://example.com It redirecting to https://www.example.com` But error is " The page isn't redirecting properly" – Rajeev Ranjan Sharma Mar 10 '15 at 12:14
  • 1
    Hello bro. I need you help again. How to make same rule for sub domain. Like `http://subdomain.example.com to https://subdomain.example.com` – Rajeev Ranjan Sharma Mar 14 '15 at 07:38
  • 1
    New error, every this is working fine but one image only showing error `The page isn't redirecting properly` and other images working fine. What to do now ?? – Rajeev Ranjan Sharma Mar 22 '15 at 08:16
  • There is no image specific rule in the answer above. You should better post a new question with more details about the URL/error and we'll attend it. – anubhava Mar 22 '15 at 08:18
  • 1
    Like `https://wwww.example.com/images/logo.png` shows a error `The page isn't redirecting properly` , but `https://www.example.com , https://www.example.com/images/ and https://www.example.com/images/someimage.png` working fine – Rajeev Ranjan Sharma Mar 22 '15 at 09:48
  • As I requested above please create a separate new question for this problem and I will try to solve it. – anubhava Mar 22 '15 at 09:50
0

just add below to your .htaccess

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Pratyush Pranjal
  • 544
  • 6
  • 26
  • 1
    When i update this rewriterule it redirect successfully to https but shows error "server not found" and some time show error "The page isn't redirecting properly", but when i remove this error and enter my domain name with https:// it working fine – Rajeev Ranjan Sharma Mar 09 '15 at 15:55
  • 1
    Above rewrite rule worked for me and it is the best way, you may have server side error or SSL error. Try in different browser or try : `RewriteEngine On RewriteCond %{HTTP_HOST} ^yourdomain.com [nocase] RewriteRule ^(.*) https://www.yourdmain.com/$1 [last,redirect=301]` – Pratyush Pranjal Mar 10 '15 at 14:17
  • 3
    i forget to say you, i am using cloudflare SSL – Rajeev Ranjan Sharma Mar 11 '15 at 12:54
0
RewriteEngine On
# ensure www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https 
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]