Replace invalid image url with 404 image - .htaccess

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?

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

<FilesMatch ".(jpg|png|gif)$">
ErrorDocument 404 "/path/to/image.jpg"
</FilesMatch>

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.

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>

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

Related

How to redirect 404 error without changing the URL in PHP when all pages are loaded with index.php

I have developed a project where all files are loaded with index.php like index.php?page=add-member in this way, so now when I am doing ind.php?page=add-member so it is showing me the 404 not found error which is quite obvious or the normal behaviour but what do I want is that I want to include my custom 404.php page but without changing the URL, so far I have used this but I am getting redirected to 404.php.
.htaccess
ErrorDocument 404 /404.php
my index.php
$page = $_GET['page'];
include 'pages.php';
include $view;
pages.php
if (!empty($page)) {
switch($page){
case "add-member":
$view = 'folders/add-member.php';
break;
}
}
So how can i do that.

htaccess Help required redirecting old deleted links

I have a site (e.g. mysite.com) and old versions of the site under a directory are still appearing in search engines (e.g. mysite.com/oldsecton/something.html). There are too many of these links to remove from search engine finds.
I want everything in the /oldsection to redirect back to mysite.com/index.html
i.e. everything in the /oldsection should go to index.html located one directory path back
I have tried different variations of .htaccess inside /oldsection such as
ErrorDocument 404 ../index.html
ErrorDocument 404 mysite.com/index.html
etc...
None of them seem to be successfull.
This should be a simple task but I keep getting an Internal Server Error or a ErrorDocument error.
Inside /oldsection/.htaccess have this code:
RewriteEngine On
RewriteRule ^ /index.html [L,R=302]

Send a 404 error via htaccess?

I'd like to cut off access to a subdirectory on my site but I want any access in the subdirectory to be a 404 error, not a 403 forbidden. How can this be accomplished?
Try:
RewriteRule ^directory/ - [L,R=404]
This redirects all requests for the folder "/directory/", they get a 404 response.
I think, 410 error better
RewriteRule ^directory/ - [L,R=410]
or my search:
RewriteCond %{HTTP_USER_AGENT} (googlebot|bingbot|Baiduspider) [NC]
RewriteRule .* - [R=410,L]
on russian wiki
404 Not Found [19] - the most common mistake when using the Internet, the main reason - an error in writing the address of a Web page. The server understood the request, but did not find the corresponding resource at the specified URL. If the server knows that there was a document at this address, then it is desirable for it to use code 410. Answer 404 can be used instead of 403 if you need to carefully hide certain resources from prying eyes. Appeared in HTTP/1.0.
Redirect 404
Put this into the .htaccess file in the directory that you want off the record. The Redirect directive is part of mod_alias and can be used to send any status code, not just redirects.
You could also for the time being change which page a user will see when confronted with a 403 error, but I wouldn't recommend doing this long-term.
.htaccess:
ErrorDocument 403 /your404pagehere.php

Need to specify full URL in .htaccess?

I have the following line in my .htaccess file and it works.
ErrorDocument 404 http://localhost/error.php?code=404
When I change it to either of these it doesn't work anymore (don't know which is correct):
ErrorDocument 404 error.php?code=404
ErrorDocument 404 /error.php?code=404
The .htaccess file and error.php are in the same directory. Why is this happening?
Note: I'm on Wampserver
FULL .HTACCESS FILE
RewriteEngine on
RewriteRule ^([a-z]+)$ $1.php
ErrorDocument 404 http://localhost/error.php?code=404
The reason this is not working is because of the query string.
When you don't supply a full URL to the ErrorDocument directive, Apache treats is as a local file path relative to DocumentRoot. Slightly confusingly, you do need to use the leading / even though it is technically a relative path.
Now, what you want to do is actually not as simple as it may seem on the face of it. Because you are now dealing with a local path, the query string portion no longer has a special meaning, and will be treated as a literal part of the file name - and obviously the file is not called error.php?code=404, it's just called error.php. Thankfully though, it is possible with a little bit of messing around, because ErrorDocument does generate an internal request which is passed through the standard routing engine. What we will need here is a little bit of mod_rewrite magic.
Try the following:
ErrorDocument 404 /error-404.php
RewriteEngine On
RewriteRule ^/?error-([0-9]+)\.php$ /error.php?code=$1 [L,QSA]
This assumes that your .htaccess and error.php files both reside in the DocumentRoot.
In my opinion, 404 (like 403, 502, ...) code is a HTTP code so it could be logic that the instruction ErrorDocument, which reference to HTTP process in your case, needs a http:// instruction.
Using DaveRandom's solution, I simply added the R flag to RwriteRule and it works.
Try this configuration:
ErrorDocument 404 /error-404.php
RewriteEngine On
RewriteRule ^/?error-([0-9]+)\.php$ /error.php?code=$1 [L,R,QSA]
For me it works on Apache 2.4.12

404 custom error template .htaccess

Ok, I have a problem, my .htaccess code is:
RewriteEngine on
ErrorDocument 404 /notfound.html
and I have not found.html near .htaccess.
Why is it not working? I don't believe what I'm seeing. I took the tutorial from here
http://www.htaccessbasics.com/404-custom-error-page/
I just want that every time I access www.site.com/ajfasoijfiajsfijaofij to show what is it in notfound.html.
when using ErrorDocument , the file you're specifying is relative to DOCUMENT ROOT not .htaccess location!
Edit:
assume you have notfound.html and you want to use it for folder dir01, you create an htaccess file inside this folder and add this:
ErrorDocument 404 /dir01/notfound.html
If you want to access your file(notfound.html) in browser it would be http://www.example.com/dir01/notfound.html. that's all I could explain!
You can use in your .htaccess file the following code:
RewriteBase /

Resources