Redirects on AWS Lightsail WordPress
Written on July 26th, 2019 by Cody SniderAWS Lightsail (Bitnami) doesn’t enable overrides using .htaccess by default and the structure of the Apache config files is, at best, confusing.
The quick and easy solution is to use:
/opt/bitnami/apache2/conf/bitnami/bitnami-apps-prefix.conf
The most common redirects are WWW to non-WWW and HTTP to HTTPS. So you don’t have to root around the internet for those, here they are. Just throw the rules below at the end of the file above.
WWW to Non-WWW
An example of this is having www.codingwithcody.com redirect to codingwithcody.com (go ahead and try it out, I use this rule on this site).
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
HTTP to HTTPS
An example of this is having http://codingwithcody.com redirect to https://codingwithcody.com (again, feel free to try it out, I use this rule on this site).
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [R,L]
All Together Now
After connecting to your machine:
sudo nano /opt/bitnami/apache2/conf/bitnami/bitnami-apps-prefix.conf
Copy and paste the rule you are looking for from above (or both, but you only need the “RewriteEngine On” line once before the rules).
And, finally, restart the Apache and PHP-FPM services:
sudo /opt/bitnami/ctlscript.sh restart
After the services come back up, you should be all set.