<< Back
How to reroute from http to https in Cakephp 3 / 4 hosted on a cpanel of AWS ec2 instance
How to reroute from http to https in Cakephp 3 / 4 hosted on a cpanel of AWS ec2 instance
How to reroute from http to https in Cakephp 3 / 4 hosted on a cpanel of AWS ec2 instance
In cakephp 3 I struggled with this. My solution was to change the first .htaccess file from
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
TO
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
Notice the 302, the reason I struggled is that the .htaccess got cached and made testing very difficult, I even went as far as delete all the cakephp code in the index.php in the webroot and just echo out 'test' Once I got it working I changed the 302 to 301 so that the .htaccess can hard cache in the browser. ( To test if values like HTTP:X-Forwarded-Proto exist I var_dump($_SERVER) in the index.php file )
IF you are on a server without HTTP:X-Forwarded-Proto
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
Published: 20th April 2020 by Gabriel Kolbe