Recently while setting up a Magento 2 store on AWS using Bitnami's Magento Stack I ran into the problem of HTTPS redirection, while visiting the site with http:// it wouldn't redirect to the https:// version, even after searching for solutions on official docs, nothing worked, however I found a simple solution that did the trick.
This guide is for advanced users who are comfortable working with core configuration files and knows how to access, edit and save these files. If you are a general user and looking to achieve the results then I would recommend you to hire a professional who can help you do it.
Coming back to the problem, so the site was accessible from both the insecure (http://) and secure (https://) protocol independently, however we intended the behavior to be such that going to the insecure URL should redirect to the secure one - unfortunately for some reasons Magento 2 doesn't provide that option in the Admin's settings.
I had to dig very deeper and had to try with multiple .conf files to finally find the one that actually work, but before that's let get an overview of the situation.
And then restarted Apache server using:
However, that didn't resolve the issue. One thing was obvious that it has to be one of the .conf or .htaccess files where I needed to add this. The next section will describe how.
This guide is for advanced users who are comfortable working with core configuration files and knows how to access, edit and save these files. If you are a general user and looking to achieve the results then I would recommend you to hire a professional who can help you do it.
Coming back to the problem, so the site was accessible from both the insecure (http://) and secure (https://) protocol independently, however we intended the behavior to be such that going to the insecure URL should redirect to the secure one - unfortunately for some reasons Magento 2 doesn't provide that option in the Admin's settings.
I had to dig very deeper and had to try with multiple .conf files to finally find the one that actually work, but before that's let get an overview of the situation.
Bitnami's Official Documentation Didn't Help
The official page where it documents how to force redirect HTTP to HTTPS didn't work. It instructs to edit the Apache virtual host configuration file at installdir/apache2/conf/bitnami/bitnami.conf (which is /opt/bitnami/apache2/conf/bitnami/bitnami.conf for Bitnami Magento stack)
In that file I added
RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [R,L]
And then restarted Apache server using:
sudo /opt/bitnami/ctlscript.sh restart apache
However, that didn't resolve the issue. One thing was obvious that it has to be one of the .conf or .htaccess files where I needed to add this. The next section will describe how.
The .conf file that worked
The official documentation contributed in confusion, however, after several attempts I found the right file to place that HTACCESS rule.
It was: /opt/bitnami/apache2/conf/bitnami/httpd.conf
I used WinSCP to log-into SFTP and access the files easily and edit it
At the end of that file add this
And save. Now use the command to restart the Apache server, now to test the site, do that in an Incognito window so you are not confused by cached pages.
This worked for me, and I hope it does for you as well, if you are facing an issue please do let me know in the comments.
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [R,L] </IfModule>
And save. Now use the command to restart the Apache server, now to test the site, do that in an Incognito window so you are not confused by cached pages.
This worked for me, and I hope it does for you as well, if you are facing an issue please do let me know in the comments.