.htaccesshttp-redirectmod-rewritehttpsquestion2answer

Redirecting whole Q2A website to HTTPS


I have been working with question2answer and I'm trying to redirect the whole website to https using .htaccess. Problem is there is rewriting going on in the file that gets messed up.

 DirectoryIndex index.php
 <IfModule mod_rewrite.c>
 RewriteEngine On
 #RewriteBase /
 RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
 RewriteRule . %1/%2 [R=301,L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]

 RewriteCond %{HTTP_HOST} !^www\.
 RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

 </IfModule>

that's what I have so far and if I add

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

it does work, but it's messing up the rewriting

 RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]

UPDATE

With this additional code this is what I get:

https://www.example.com/index.php?qa-rewrite=123/this-is-a-title

should be:

https://www.example.com/123/this-is-a-title

Solution

  • Order of rewrite rules in .htaccess is very important as rules are executed from top to bottom.

    Try this code instead:

     DirectoryIndex index.php
     <IfModule mod_rewrite.c>
     RewriteEngine On
     #RewriteBase /
    
     RewriteCond %{HTTP_HOST} !^www\. [NC]
     RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
    
     RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
     RewriteRule . %1/%2 [R=301,L]
    
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteRule ^.*$ index.php?qa-rewrite=$0 [L,QSA]
    
     </IfModule>