Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+6 votes
1.8k views
in Q2A Core by
How to redirect HTTP to HTTPS when using default (URL Structure option 1 chosen) htaccess file?

URL structure:
/123/why-do-birds-sing (requires htaccess file) 

My .htaccess file:

Options -Indexes
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]
</IfModule>


Q2A version: 1.7.4

3 Answers

+3 votes
by
selected by
 
Best answer

Add the following :

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://blog.drhack.net/$1 [R,L]
Just above </IfModule>
that should redirect all your http requests to https .. and change the website address to your website
by
i dont know whats wrong with formatting here :/ it was fine before ..
+3 votes
by

Options -Indexes
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} libwww-perl.* 
RewriteRule .* ? [F,L]
SetEnvIfNoCase User-Agent "^libwww-perl*" block_bad_bots
Deny from env=block_bad_bots
#RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.YOURDOMAIN.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.YOURDOMAIN.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
</IfModule>

This one will redirect to https://www with showing 404 page correctly. 

By simply adding the below code Your 404 page will not be shown.

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.YOURDOMAIN.com/$1 [R=301,L]

So use the above one.

Correct me if you have any better suggestions. I would like to know how pokemondb.net is using the htaccess file. it may be better than this.

0 votes
by

Options -Indexes
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#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]
</IfModule>

...