开发者

Redirect 404 hotlinked images to a single image?

Basically a friends just gifted me an old domain to develop, it has quite a few inbound links and its getting a lot of 404 hits.

The basis of the site is to let users hotlink images off it, to put 开发者_运维百科in forum signatures / blog comments and whatnot. I was wondering if there was a way to redirect all these broken image links to a single image which will show up on their page? (basically with a little message saying the sites back up with new images!)

I'm developing the new site on Kohana and using CPanel to admin the hosting (if any of this helps).


Well, i guess, since you're using PHP, that there's some sort of "retrieve image based on ID" function.

If you're storing your images on a directory try using

file_exists('path/to/my/img.png') ? $image = 'path/to/my/img.png' : $image = 'path/to/my/default/img.png';

If you're storing them in a DB, follow the same logic. Instead of using file_exists() check if the query returned something.


If mod_rewrite is available, you can add a rewrite rule for all pictures not corresponding to any file names, together with a RewriteCond to filter out requests from your own site according to HTTP_REFERER. I think this article will explain this in more detail for you.


The simplest method (on Apache) is:

 ErrorDocument 404 /url/pointing/to/image/you/want/to/serve.jpg

But this will serve up the image for ALL 404s, within the vhost/location/directory you specify this handler for, so a simple typo on a legitimate request will still get this image.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜