title: htaccess redirect rule for Amazon CloudFront IPs
body: <p>I have setup a CDN plugin on a joomla website using CloudFront.</p>
  
  <p>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.</p>
  
  <p>I have calculated the IP range used by CloudFront as per this post:
  <a href="https://forums.aws.amazon.com/ann.jspa?annID=910" rel="nofollow">https://forums.aws.amazon.com/ann.jspa?annID=910</a></p>
  
  <p>And I have obtained the following IP ranges:</p>
  
  <p>HostMin: 54.239.128.1
  HostMax: 54.239.191.254</p>
  
  <p>HostMin: 54.240.128.1
  HostMax: 54.240.191.254</p>
  
  <p>HostMin: 204.246.168.1
  HostMax: 204.246.171.254</p>
  
  <p>HostMin: 204.246.174.1
  HostMax: 204.246.175.254</p>
  
  <p>HostMin: 204.246.176.1
  HostMax: 204.246.191.254</p>
  
  <p>HostMin: 205.251.192.1
  HostMax: 205.251.223.254</p>
  
  <p>HostMin: 205.251.249.1
  HostMax: 205.251.249.254</p>
  
  <p>HostMin: 205.251.250.1
  HostMax: 205.251.251.254</p>
  
  <p>HostMin: 205.251.252.1
  HostMax: 205.251.253.254</p>
  
  <p>HostMin: 205.251.254.1
  HostMax: 205.251.254.254</p>
  
  <p>HostMin: 216.137.32.1
  HostMax: 216.137.63.254</p>
  
  <p>Finally I put together the following RewriteCond to stop the rules from redirecting requests coming from CloudFront:</p>
  
  <pre><code>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}
  </code></pre>
  
  <p>However it does not seem to work and CloudFront requests are still redirected to the www version of the website.</p>
  
  <p>Anyone, any suggestion?
  Thank you!</p>