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개 답변
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?

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인