views:

310

answers:

2

Basically, I'm moving all my images over to s3, but will have tons of old references to old image locations on my website. Looking to rewrite anything in the wp-content/uploads/ folder to an s3 address.

Short story is I'm not very good with regular expressions, and have been at it a while already. Any advice or help would be greatly appreciated.

edit:

here's what I've got so far:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php


RewriteRule ^wp-content/uploads/(.*)$ http://example.s3.amazonaws.com/wp-content/uploads/$1 [L]
</IfModule>
+1  A: 

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.so
LoadModule 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).

Daniel Vassallo
This seems like a great solution, but unfortunately I am on a shared hosting plan where I do not have access to such things :(
Brian
Oh, that leaves URL Rewriting as the only option then. I'll leave the answer here, just in case it may help someone else in a similar situation.
Daniel Vassallo
Yeah, definitely. I may come back to it in the future if I stop slumming it up and move to better hosting. Any advice on the mod_rewrite based on my attempt above?
Brian
A: 

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].

Max Shawabkeh
Isn't the ./index.php rule required for wordpress to function correctly? or am I just talkin' our of my ass?
Brian
Yes, it is. You might want to put your rule ABOVE that rule.
Chacha102
Actually, you will need to move it above both the file checking lines as well, as if not, it will ignore the rules and just go to the physical file.
Chacha102
Chacha, you should post those as an answer, as they're better than mine, which looks at the problem from a wordpressless perspective.
Max Shawabkeh