2

How can I redirect HTTP to https include WWW using .htaccess?

Example :

  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

I'm trying

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] 
6
  • It would be great if you could show us what have you tried! Commented Feb 13, 2019 at 9:13
  • @M.K post updated with code Commented Feb 13, 2019 at 9:20
  • Please don't use unneccessary tags - this question is in no way related to JS, PHP, or HTML Commented Feb 13, 2019 at 9:40
  • 1
    Possible duplicate of How to redirect all HTTP requests to HTTPS Commented Feb 13, 2019 at 9:40
  • @NicoHaase this not duplicate. you can call it similar question & i use the tag which stackoverflow suggest me. Commented Feb 13, 2019 at 9:42

4 Answers 4

3

You can put the following code inside your .htaccess file

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] 

This .htaccess file will redirect http://example.com/ to https://example.com/.

Code Explanation:

  • [NC] matches both upper and lower case versions of the URL
  • The X-Forwarded-Proto (XFP) header is a de-facto standard header for identifying the protocol
Sign up to request clarification or add additional context in comments.

Comments

0
RewriteEngine On RewriteCond %{HTTP_HOST} ^example\.com [NC] RewriteCond %{SERVER_PORT} 80 //If you want the condition to match port RewriteRule ^(.*)$ https://www.example.com/$1 [R,L] 

Try in this. Hope this may help you.

1 Comment

Please add some explanation to these rules - "try this" does not help the OP to learn what you've changed and why this change makes a difference
0

Try this

RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L] 

1 Comment

Please add some explanation to these rules - "try this" does not help the OP to learn what you've changed and why this change makes a difference
0

APACHE

RewriteCond %{HTTPS} off RewriteCond %{HTTPS_HOST} !^www.example.com$ [NC] RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301] 

NGINX

server { listen 443 ssl; server_name example.com; ssl_certificate /srv/www/example.com/keys/ssl.crt; ssl_certificate_key /srv/www/example.com/keys/www.example.com.key; return 301 https://www.example.com$request_uri; } 

You can replace ssl on; directive with listen 443 ssl; as recommendation from nginx documentation.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.