Redirect images & mp3s to mirror via htaccess
June 26, 2008 9:13 PM Subscribe
How do I redirect all my website's images and mp3s to another domain via .htaccess?
I have a domain (foo.com) that ordinarily serves my website and its images and mp3s. I have a mirror (bar.com) that has a mirror/backup of all those images and mp3s.
In the event of a traffic surge on foo.com, I want to make Apache serve all my images and mp3s from bar.com.
All I can find is examples to stop others from hot-linking my files, which isn't what I want.
(Bonus aside question: Am I not turning up examples of this because it's better done in httpd.conf some way?)
I have a domain (foo.com) that ordinarily serves my website and its images and mp3s. I have a mirror (bar.com) that has a mirror/backup of all those images and mp3s.
In the event of a traffic surge on foo.com, I want to make Apache serve all my images and mp3s from bar.com.
All I can find is examples to stop others from hot-linking my files, which isn't what I want.
(Bonus aside question: Am I not turning up examples of this because it's better done in httpd.conf some way?)
Best answer: Actually, that would break with images that have a . in their name (before the extension) - this should be acceptable too:
RewriteRule ^/(.+(\.jpg|\.png|\.gif|\.jpeg))$ http://bar.com/$1 [L]
posted by sycophant at 9:58 PM on June 26, 2008
RewriteRule ^/(.+(\.jpg|\.png|\.gif|\.jpeg))$ http://bar.com/$1 [L]
posted by sycophant at 9:58 PM on June 26, 2008
Best answer: DNS changes are so fast right now, that one option might be to have the following, let's say your 2 servers have IP addresses 1.2.3.4 and 5.6.7.8:
foo.com ==> 1.2.3.4
images.foo.com ==> 1.2.3.4
When load gets high, log into your domain registrar account and change DNS (assuming you have access to this.. Godaddy and most others provide it)
images.foo.com ==> 5.6.7.8
Alternatively, you could use mod_alias in apache, seen here... Check out the section on "redirect":
The Redirect directive maps an old URL into a new one by asking the client to refetch the resource at the new location.
The old URL-path is a case-sensitive (%-decoded) path beginning with a slash. A relative path is not allowed. The new URL should be an absolute URL beginning with a scheme and hostname.
Example:
Redirect /service http://foo2.bar.com/service
If the client requests http://myserver/service/foo.txt, it will be told to access http://foo2.bar.com/service/foo.txt instead.
posted by twiggy at 9:59 PM on June 26, 2008
foo.com ==> 1.2.3.4
images.foo.com ==> 1.2.3.4
When load gets high, log into your domain registrar account and change DNS (assuming you have access to this.. Godaddy and most others provide it)
images.foo.com ==> 5.6.7.8
Alternatively, you could use mod_alias in apache, seen here... Check out the section on "redirect":
The Redirect directive maps an old URL into a new one by asking the client to refetch the resource at the new location.
The old URL-path is a case-sensitive (%-decoded) path beginning with a slash. A relative path is not allowed. The new URL should be an absolute URL beginning with a scheme and hostname.
Example:
Redirect /service http://foo2.bar.com/service
If the client requests http://myserver/service/foo.txt, it will be told to access http://foo2.bar.com/service/foo.txt instead.
posted by twiggy at 9:59 PM on June 26, 2008
sycophant's idea of using mod_rewrite is also a good one. You'd need to ensure you have mod_rewrite installed, which you may or may not.
mod_alias is almost definitely installed, and you could use redirectmatch (see right below redirect in the link I provided) to use regular expression matches like those sycophant provided.
posted by twiggy at 10:02 PM on June 26, 2008
mod_alias is almost definitely installed, and you could use redirectmatch (see right below redirect in the link I provided) to use regular expression matches like those sycophant provided.
posted by twiggy at 10:02 PM on June 26, 2008
Best answer: The problem is, if you use a redirect then the main server will still be dealing with a request for each file, so you won't be getting the full benefit of offloading to another domain.
If you can, change all the image/mp3 URLs to another (sub)domain and then adjust DNS to switch over as suggested by twiggy. If you keep the TTL reasonably low then you can switch quickly when needed, rather than waiting hours/days for propagation.
posted by malevolent at 12:10 AM on June 27, 2008
If you can, change all the image/mp3 URLs to another (sub)domain and then adjust DNS to switch over as suggested by twiggy. If you keep the TTL reasonably low then you can switch quickly when needed, rather than waiting hours/days for propagation.
posted by malevolent at 12:10 AM on June 27, 2008
Response by poster: malevolent: Good point.
Thanks for the answer's I'm pretty sure my server can handle the requests, it's just the data I was worried about. But twiggy's DNS option is probably the smartest so I'll have to look into that.
As a followup: are there any decent guides for setting that kind of thing up? I know it's how the majority of large sites do it.
posted by frenetic at 7:18 AM on June 27, 2008
Thanks for the answer's I'm pretty sure my server can handle the requests, it's just the data I was worried about. But twiggy's DNS option is probably the smartest so I'll have to look into that.
As a followup: are there any decent guides for setting that kind of thing up? I know it's how the majority of large sites do it.
posted by frenetic at 7:18 AM on June 27, 2008
This thread is closed to new comments.
RewriteRule ^/([^\.]+(\.jpg|\.png|\.gif|\.jpeg))$ http://bar.com/$1 [L]
posted by sycophant at 9:56 PM on June 26, 2008