How To Mirror Files?

Redirect images & mp3s to mirror via htaccess

  • 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?)

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

frenetic at Ask.Metafilter.Com Visit the source

Was this solution helpful to you?

Other answers

Off the top of my head, something like: RewriteRule ^/([^\.]+(\.jpg|\.png|\.gif|\.jpeg))$ http://bar.com/$1 [L]

sycophant

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 http://httpd.apache.org/docs/2.0/mod/directive-dict.html#Description 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.

twiggy

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.

twiggy

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.

malevolent

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.

frenetic

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.