exclude directory from custom error page

New to mod_rewrite? This is a good place to start.

exclude directory from custom error page

Postby hessodreamy » Thu Nov 29, 2007 3:47 am

My htaccess in root specifies a custom 404 page
ErrorDocument 404 http://www.mydomain.com/404e.php

but in the case of missing images, I want to just return a standard 404. How do I exlude images from the custom 404? (either by file extension matching or by exluding a particular directory)
Cheers
hessodreamy
 
Posts: 15
Joined: Wed Feb 28, 2007 2:04 am

Postby richardk » Thu Nov 29, 2007 3:32 pm

Try adding
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule \.(jpe?g|bmp|gif|png)$ - [NC,R=404,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby hessodreamy » Fri Nov 30, 2007 1:41 am

Thanks but that didn't work. Was that to go in a new htaccess in the images directory or to be added to the existing htaccess in root?
Here's the full htaccess in root. I tried adding your code at different points but no joy:
Code: Select all
ErrorDocument 404 http://www.mydomain.com/404e.php
ErrorDocument 500 http://www.mydomain.com/404e.php
Options +FollowSymLinks
RewriteEngine on

RewriteCond %{SERVER_PORT}s ^(80s|443(s))$
RewriteCond %2-%{HTTP_HOST} ^(s?)-(mydomain\.com)$ [NC]
RewriteRule ^(.*)$ http%1://www.%2/$1 [R=301,L]
hessodreamy
 
Posts: 15
Joined: Wed Feb 28, 2007 2:04 am

Postby richardk » Fri Nov 30, 2007 4:04 pm

Thanks but that didn't work.

It still showed the custom 404 page?

Was that to go in a new htaccess in the images directory or to be added to the existing htaccess in root?

It shouldn't matter.

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule \.(jpe?g|bmp|gif|png)$ /default404.php [NC,L]

Then create a file called default404.php with the following in it
Code: Select all
<?php

header('Content-Type: text/html', true, 404);
echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">' . "\n"
   . '<html><head>' . "\n" . '<title>404 Not Found</title>' . "\n"
   . '</head><body>' . "\n" . '<h1>Not Found</h1>' . "\n"
   . '<p>The requested URL ' . getenv('REQUEST_URI')
   . ' was not found on this server.</p>' . "\n" . '</body></html>';
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby hessodreamy » Fri Dec 18, 2009 6:41 am

I know it's an old thread but I revisited the problem and it's actually pretty easy. In a way. You can specify separate custom 404 pages using a new htaccess file in the directory (this will also affect subdirectories).

So, im my case, I put this in .htaccess in the images directory:
Code: Select all
ErrorDocument 404 /images/404.html
ErrorDocument 500 /images/404.html

And created a simple html file at /images/404.html

Basically this avoids any processing that takes place on your custom 404, when there's no need because it's being served due to a missing image.
hessodreamy
 
Posts: 15
Joined: Wed Feb 28, 2007 2:04 am


Return to Beginner's Corner

Who is online

Users browsing this forum: Google [Bot] and 17 guests

cron