Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.

How to redirect http://example.com and https://example.com to https://www.example.com via htaccess

+1 vote
55 views
asked Dec 1 in Q2A Core by MKDan
Options -Indexes
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{HTTP_HOST} ^mysite.com [NC] ---------------------> Added by me
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301,NC] --------> Added by me
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$ --------------- All these are already present on the htaccess file
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
</IfModule>

The rule RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L] is creating the problem.

2 Answers

0 votes
answered Dec 11 by chongshengdz

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [L,R=301]

place the above 2 lines into your .htaccess after #RewriteBase /

0 votes
answered 5 days ago by fahimalfarhan
RewriteCond %{HTTP_HOST} !^www.yoursite.com$ [NC]
RewriteRule ^(.*)$ https://www.yoursite.com/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
...