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!

Univate
已提問 3 個月前檢視次數 156 次
2 個答案
1
已接受的答案

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
已回答 3 個月前
profile picture
專家
已審閱 2 個月前
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
專家
已回答 3 個月前
  • 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?

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南