mod_rewrite to move Wordpress images to Amazon S3 mod_rewrite to move Wordpress images to Amazon S3 wordpress wordpress

mod_rewrite to move Wordpress images to Amazon S3


Instead of using mod_rewrite, you could also set up a reverse proxy using mod_proxy. This should be easier configure -- no regular expressions required.

The fundamental mod_proxy directive to set up a reverse proxy is the ProxyPass. You would typically add the following line to your local Apache configuration file (usually httpd.conf or apache2.conf):

ProxyPass  /wp-content/uploads/   http://example.s3.amazonaws.com/wp-content/uploads/

In this case, the browser would be requesting http://your-domain.com/wp-content/uploads/abc.png but your web server would serve this by acting as a proxy to http://example.s3.amazonaws.com/wp-content/uploads/abc.png.

You also need to make sure to have the following configuration lines uncommented in your Apache config file:

LoadModule proxy_module /usr/lib/apache2/modules/mod_proxy.soLoadModule proxy_http_module /usr/lib/apache2/modules/mod_proxy_http.so

Make sure to restart your local Apache service after you do any changes to the config file.

In addition, make sure to set up your Amazon S3 buckets for Virtual Hosting (Amazon Documentation).


I arrive to a intermediate solution.Using a reverse proxy, buy using a rewrite rule.The only thing you should add is the [P] at the end of your rewrite rule.For example:

RewriteRule ^wp-content/uploads/(.*)$ http://example.s3.amazonaws.com/wp-content/uploads/$1 [P]

Take a look at: http://httpd.apache.org/docs/2.0/mod/mod_proxy.html#forwardreverse

You should also check the following configuration lines uncommented in your Apache config file, like Daniel Vassallo said:

LoadModule proxy_module /usr/lib/apache2/modules/mod_proxy.soLoadModule proxy_http_module /usr/lib/apache2/modules/mod_proxy_http.so


In your example, the RewriteRule . /index.php line is unnecessary and problematic as it will cause an infinite loop (the [L] ("last") modifier is not quite what it sounds like), but otherwise it should work as intended. You might want to do a permanent redirect too, by replacing [L] with [R=301].