superuseroi
superuseroi

Reputation: 1348

htaccess redirect rule for Amazon CloudFront IPs

I have setup a CDN plugin on a joomla website using CloudFront.

All seems to be working fine, except all request from CloudFront are redirected to the www version of the website domain. It seems this is due to the non-www -> www redirec rule.

I have calculated the IP range used by CloudFront as per this post: https://forums.aws.amazon.com/ann.jspa?annID=910

And I have obtained the following IP ranges:

HostMin: 54.239.128.1 HostMax: 54.239.191.254

HostMin: 54.240.128.1 HostMax: 54.240.191.254

HostMin: 204.246.168.1 HostMax: 204.246.171.254

HostMin: 204.246.174.1 HostMax: 204.246.175.254

HostMin: 204.246.176.1 HostMax: 204.246.191.254

HostMin: 205.251.192.1 HostMax: 205.251.223.254

HostMin: 205.251.249.1 HostMax: 205.251.249.254

HostMin: 205.251.250.1 HostMax: 205.251.251.254

HostMin: 205.251.252.1 HostMax: 205.251.253.254

HostMin: 205.251.254.1 HostMax: 205.251.254.254

HostMin: 216.137.32.1 HostMax: 216.137.63.254

Finally I put together the following RewriteCond to stop the rules from redirecting requests coming from CloudFront:

RewriteCond %{REMOTE_ADDR} !^204\.246\.16([4567])\.\d{1,3}
RewriteCond %{REMOTE_ADDR} !^54\.2(39|40)\.(12[89]|1[3-8][0-9]|19[01])\.\d{1,3}
RewriteCond %{REMOTE_ADDR} !^204\.246\.(16[89]|17[01])\.\d{1,3}
RewriteCond %{REMOTE_ADDR} !^204\.246\.1(7[4-9]|8[0-9]|9[01])\.\d{1,3}
RewriteCond %{REMOTE_ADDR} !^54\.239\.(249|25[0-4])\.\d{1,3}
RewriteCond %{REMOTE_ADDR} !^205\.251\.(19[2-9]|2[01][0-9]|22[0-3])\.\d{1,3}
RewriteCond %{REMOTE_ADDR} !^216\.137\.(3[2-9]|[45][0-9]|6[0-3])\.\d{1,3}

However it does not seem to work and CloudFront requests are still redirected to the www version of the website.

Anyone, any suggestion? Thank you!

Upvotes: 0

Views: 1456

Answers (1)

jamieb
jamieb

Reputation: 10033

Add both "www" and non "www" hostnames as an origin for your Cloudfront distribution. Get rid of the rewrite rules. This should solve your problem.

Upvotes: 1

Related Questions