Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing HTTPS on All Traffic and All Pages

I have a Codeigniter application which was developed and tested on the normal http. We recently moved to SSL certificate and all page the website doesn't redirect properly in .htaccess.it done only for front page http://grdagrd.com/. but it does not work for other pages: Sonbol Roud dam

How Forcing HTTPS on All Traffic and All Pages for base url https://grdagrd.com/?

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^([\s\S]*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
like image 629
g.dlta Avatar asked Mar 17 '26 05:03

g.dlta


2 Answers

Your thinking was correct to solve the problem, but regex was NOT correct. We need NOT to match everything here, since whole URL itself we need to change to https hence .* will work while rewriting rule. Could you please try following, if you have any other Rules too then keep these rules at very first/starting of .htaccess file. In case RewriteCond %{HTTPS} !on doesn't work then change it to RewriteCond %{HTTPS} off. Please make sure you clear your cache of your browser before you test URLs.

RewriteEngine ON
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L]


With OP's shown .htaccess it could be as follows, its a simple copy/paste from OP's question + my rules combinations, since I am not sure what exactly OP is dealing with other rules(moreover that is outside of focus of the question too).

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\s\S]*)$ /index.php [L,B,QSA]
</IfModule>
like image 111
RavinderSingh13 Avatar answered Mar 19 '26 14:03

RavinderSingh13


After installing an SSL certificate, your website is available over HTTP and HTTPS. However, it’s better to use only the latter because it encrypts and secures your website’s data.

you can see the full tutorial here https://tutsfx.com/force-https-traffic-using-htaccess/

Forcing HTTPS on All Traffic

RewriteEngine On 
RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

IMPORTANT: Make sure that the line RewriteEngine On is not repeated twice. In case the line already exists, simply copy the rest of the code without it.

like image 31
flutterbloc Avatar answered Mar 19 '26 14:03

flutterbloc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!