用 404 图片替换无效图片 url

发布于 2024-09-28 16:00:18 字数 743 浏览 0 评论 0原文

我不知道这是否可能,我有一个我自己制作的图像主机。我需要对其进行一些最后的调整。

每当图像被删除或者是无效的图像 url 时,它应该替换为 404 图像,例如,如果有人添加以下内容:

http://imagehosturl.com/i/34njk5n.jpg

但这是一个无效链接,所以我需要它显示:

http://imagehosturl.com/img/notfound.jpg

这是这样的:

替代文本 http://tinypic.com/images/404.gif

我确实知道 .htaccess 可以用它的 ErrorDocument 404 来做到这一点,但是当用户访问无效页面时我已经有了一个,所以它会显示 404 页面。

因此,每当用户热链接图像并且该图像无效或被删除时,我需要将其替换为 404 图像。

我怎样才能做到这一点?

I don't know if this is possible or not, I have an image host that I've made myself. I need some last tweaks with it.

Whenever an image has been deleted or is an invalid image url, it should replace with an 404 image, so for example if someone adds this:

http://imagehosturl.com/i/34njk5n.jpg

But it's an invalid link, so I need it to show:

http://imagehosturl.com/img/notfound.jpg

Which is like this:

alt text http://tinypic.com/images/404.gif

I do know that .htaccess can do this with it's ErrorDocument 404, but I have one already when a user access to an invalid page, so it would show the 404 page.

So whenever a user hotlinks an image and it's invalid or is deleted, I need it to be replaced with the 404 image.

How can I make this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你不是我要的菜∠ 2024-10-05 16:00:18

这是一个可能的答案:

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule \.(gif|jpe?g|png|bmp) /path/to/logo.gif [NC,L]

另一种方法是使用自定义脚本页面:

使用errorDocument指令
(记录在 [httpd.apache.org ])
将“404”错误指向脚本(perl、
PHP,无论如何)。如果请求的文件
有图像扩展名(或有
图像/* mimetype; PHP 提供了
mime_content_type [us2.php.net]
为此功能;我确信有
在 Perl 中,有很多方法可以做到这一点;这
MIME::Types [search.cpan.org] 模块
是一种方式),然后设置
mimetype 的“Content-Type”标头
您的徽标图像并返回
徽标图像的内容
浏览器。

http://www.webmasterworld.com/forum92/3458.htm

Here's one potential answer:

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule \.(gif|jpe?g|png|bmp) /path/to/logo.gif [NC,L]

Another is to use a custom scripted page:

Use the errorDocument directive
(documented at [httpd.apache.org ]) to
point a '404' error to a script (perl,
PHP, whatever). If the requested file
has an image extension (or has an
image/* mimetype; PHP supplies the
mime_content_type [us2.php.net]
function for this; I'm sure there are
many ways to do this in perl; the
MIME::Types [search.cpan.org] module
is one way), then set the
"Content-Type" header to the mimetype
of your logo image and return the
content of the logoimage to the
browser.

http://www.webmasterworld.com/forum92/3458.htm

流殇 2024-10-05 16:00:18
<FilesMatch ".(jpg|png|gif)$">
ErrorDocument 404 "/path/to/image.jpg"
</FilesMatch>
<FilesMatch ".(jpg|png|gif)$">
ErrorDocument 404 "/path/to/image.jpg"
</FilesMatch>
月牙弯弯 2024-10-05 16:00:18

使用 Apache,您可以拥有多个 .htaccess 文件。因此,如果您的所有图像都存储在同一目录中,请在该目录内创建一个 .htaccess 文件并添加

ErrorDocument 404 /img/notfound.jpg

这将创建一个仅应用于您的图像目录及其子目录。

With Apache, you can have multiple .htaccess files. So, if all of your images are stored in the same directory, create an .htaccess file inside of that directory and add

ErrorDocument 404 /img/notfound.jpg

This will create a custom 404 redirect that is applied only to your image directory, plus its subdirectories.

最冷一天 2024-10-05 16:00:18

您可以将 ErrorDocument 与 FilesMatch 指令一起使用,

<filesMatch "\.(jpg|png|gif)$">
ErrorDocument 404 /image.jpg 
</filesMatch>

如果 404 图像 uri 包含 jpg pnggif/image.jpg请求延期。

您还可以将自定义图像或 html 标记添加到错误文档中:

<filesMatch "\.(jpg|png|gif)$">
ErrorDocument 404 '<img src="image.jpg">'
</filesMatch>

You can use ErrorDocument with FilesMatch directive

<filesMatch "\.(jpg|png|gif)$">
ErrorDocument 404 /image.jpg 
</filesMatch>

This will show /image.jpg if a 404 image uri with jpg png or gif extension is requested.

you can also add your custom image or html markup to the errordocument :

<filesMatch "\.(jpg|png|gif)$">
ErrorDocument 404 '<img src="image.jpg">'
</filesMatch>
愛放△進行李 2024-10-05 16:00:18

根据 AdamH 的回答,您还应该输出 404 header。这是我正在使用的,

.htaccess

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule \.(gif|jpe?g|png|bmp) /upload/image404.php [NC,L]

image404.php

<?php
    $file = 'image404.jpg';
    $type = 'image/jpeg';
    header("HTTP/1.0 404 Not Found");
    header('Content-Type:'.$type);
    header('Content-Length: ' . filesize($file));
    readfile($file);
?>

According and in addition to AdamH's answer, you should output 404 header. Here's what I'm using,

.htaccess

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule \.(gif|jpe?g|png|bmp) /upload/image404.php [NC,L]

image404.php

<?php
    $file = 'image404.jpg';
    $type = 'image/jpeg';
    header("HTTP/1.0 404 Not Found");
    header('Content-Type:'.$type);
    header('Content-Length: ' . filesize($file));
    readfile($file);
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文