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?

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则