Reputation: 416
I am running into an issue where my website www.example.com is mapped to AWS Cloudfront. However, I'd like to also have Cloudfront CDN pass the Host "www.example.com" to the origin server to retrieve content. It doesn't look like this feature is available on Cloudfront but pretty sure it is on other CDN providers like LLNW etc.
Now to get around this, I can use the A record of "example.com" as an origin on my server side BUT this can easily cause a duplicate content issue and I'm already redirecting non-www to www in my Apache config:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^/(.*)?$ http://www.example.com/$1 [R=301,NC,NE,L]
Is there a way to have the rule above for all EXCEPT *.cloudfront.net domains? This way it can ensure that any traffic coming from cloudfront.net would be able to use "example.com" as origin without getting redirected to "www.example.com", thus causing a loop issue.
Here is the rule that I had put in place:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^.cloudfront\.net$ [NC]
RewriteRule ^/(.*)?$ http://www.example.com/$1 [R=301,NC,NE,L]
However, it does not work as I had hoped.
Any suggestions would be appreciated.
Thanks!
Upvotes: 2
Views: 2141
Reputation: 41838
Your second condition has a small bug.
!^.cloudfront\.net
is anchored, and therefore specifies that the host must be not match exactly one character (specified by the .
) followed by cloudfront.net
. This will never match, so the condition is always true, and cloudfront is not excluded.
Replace that condition by an unanchored one, so that your two conditions become:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{HTTP_HOST} !cloudfront\.net$ [NC]
Upvotes: 2