Using Apache to redirect requests to CloudFront

If you have some static content you want to send to CloudFront, and you don’t want to redo all your hrefs to handle that for you, here’s a quick and dirty way to use Apache instead.

First, setup your CloudFront distribution. Once your distribution is active, make a note of its URL. Alternatively you could be using a custom hostname, but for now we will pretend that your distribution’s name is “”.

Let’s also assume that you want to have cloudfront handle all non-php files.

Now add the following lines to the apache config for the site :

RewriteCond %{HTTP_USER_AGENT} !Amazon\ CloudFront$
RewriteCond %{REQUEST_FILENAME} !\.(php)$ [NC]
RewriteRule /(.*)$1 [R,L]

In this example I’m using SSL for the vhost, but it works just as well for non-SSL traffic. Just setup the CloudFront distribution to match the viewer protocol.

Using Apache to redirect requests to CloudFront