Lightsail Bitnami Wordpress with CloudFront www. Redirect

0

Hello, I'm using Lightsail to host Bitnami Wordpress. I've configured a CloudFront distribution within Lightsail to serve the cached content from the origin server with 2 valid domains example.com and www.example.com.

The issue I'm having is I now have 2 domains (www and non-www) serving the same content with no redirection occurring. I was able to get https redirect to occur, after editing /opt/bitnami/wordpress/wp-config.php with the following:

define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST'] . '/');
define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST'] . '/');
if (isset($_SERVER['HTTP_CLOUDFRONT_FORWARDED_PROTO'])
&& $_SERVER['HTTP_CLOUDFRONT_FORWARDED_PROTO'] === 'https') {
$_SERVER['HTTPS'] = 'on';
}

Now, I'm trying to get www to non-www redirect to work. I've tried to add the following to opt/bitnami/wordpress/.htaccess but it seems as if it's not respecting the alterations.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301,NC]

Bottom line - what changes do I need to make to either routing (within the Lightsail DNS zone I have mapped) or to the origin server to have a simple www.example.com redirect to example.com? Thank you!

2 Answers
1
Accepted Answer

After playing around with it, I was able to get a successful redirect by editing opt/bitnami/apache/conf/vhosts/00_status-vhost.conf with your above code:

Define CUSTOM_DOMAIN_NAME "example.com"
RewriteEngine On
<VirtualHost *:80 *:443>
    ServerName www.example.com
    RewriteRule ^/(.*) https://${CUSTOM_DOMAIN_NAME}/$1 [R=301,L]
</VirtualHost>

All seems to be working now! Thank you!

Univate
answered 2 months ago
profile picture
EXPERT
reviewed a month ago
1

Hello.

Can I redirect if I configure Apache as below?(After making the settings, restart Apache.)

Define CUSTOM_DOMAIN_NAME "example.com"
RewriteEngine On
<VirtualHost *:80 *:443>
    ServerName www.example.com
    RewriteRule ^/(.*) https://${CUSTOM_DOMAIN_NAME}/$1 [R=301,L]
</VirtualHost>
profile picture
EXPERT
answered 2 months ago
  • Thanks for the suggestion, but unfortunately, it doesn't seem to have worked.

    I edited this file/opt/bitnami/apache/conf/httpd.conf and added the virtual host code (after swapping out my domain for example.com), saved the file, restarted Apache, reset my CloudFront cache too for good measure, and no luck. I've tried from multiple browsers/computers too, double checked for redirects using a 3rd party, and no dice.

    Is that the correct file?

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions