We are installing a Java web application in a EC2 running Tomcat. The application uses a folder for users pictures (static content)-> www.example.com/PICTURES

We use mod_jk to exclude Tomcat from serving the path /PICTURES so it is Apache who serve it.

We are planning to move /PICTURES to S3 and serve them from our bucket.

In our code we use short urls,so for example, a user picture is defined as: /PICTURES/1.jpg

We want to redirect every call from www.example.com/PICTURES/* to our bucket -> example.com.s3-website-us-east-1.amazonaws.com/PICTURES

We have been searching for the best strategy but it seems the are not an unified solution.

As far I know we should use:

a) mod_rewrite: creating regex for every static content we would like to redirect to ur bucket

b) proxy: work as a proxy to S3

Is there any other solution? What are the pros and cons?


Camilo create or edit your .htaccess file and add the lines:

RewriteEngine On
RewriteRule ^/PROFILES/(.+) http://pictures.example.com/PROFILES/$1 [R,L]

The best way to do this is to use a different domain for static content (e.g. static.example.com) which points to S3 or CloudFront. Proxying to S3 isn't going to buy you anything, and redirecting is going to add extra latency to every request.

  • Great answer. As a bonus, you may be able to serve static.example.com as a cookieless subdomain, if you're careful. – ceejayoz Feb 14 '13 at 19:42
  • Thanks @mgorven! I got it, and we have prepared a cname in our DNS to redirect to static.example.com. The problem is: how can we achieve this in our code? Right now we do something like this: user.getPicture and that return /pictures/user.jpg and therefore the server insert this code in the html: http://example.com/pictures/user.jpg but what we really want is: http://static.example.com/pictures/user.jpg How can we do it? – Camilo Lopez-De Felipe Feb 15 '13 at 10:38
  • @CamiloLopez-DeFelipe You'll need to change your application to support that. – mgorven Feb 15 '13 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.