Web Hosting Talk


Go Back   Web Hosting Talk : Web Hosting Main Forums : Hosting Security and Technology : .htaccess missing image replacement
Reply

Hosting Security and Technology Configuring and optimizing web hosting servers and operating systems, developing administration scripts, building servers, protecting against hackers, and general security (SSL certificates, etc.)

 
Thread Tools Search this Thread Display Modes
  #1  
Old 12-20-2007, 11:36 PM
EverSpeed EverSpeed is offline
View Beta Profile
Junior Guru Wannabe
 
Join Date: Feb 2004
Posts: 53
.htaccess missing image replacement

I have been fiddling with an .htaccess file trying to get it to show a default "Image Not Available" image in place of a 404 error. I have found code in other forums that has worked for others, but does nothing for me:

Code:
Options +FollowSymLinks 
RewriteEngine on 
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}!-f 
RewriteCond %{REQUEST_URI} (.*).(gif|jpg|jpeg|png)$ 
RewriteRule ^(.*) noimage.gif
and this one which several people said worked for them

Code:
RewriteCond %{ENV:ERROR404} \.(jpg|gif|png)$ [NC]
RewriteRule ^.*$ noimage.gif [L]
Anyone had experience with this? The webserver is Apache on CentOS 5.

Thanks,
Aaron

Reply With Quote
Sponsored Links
  #2  
Old 12-21-2007, 12:11 AM
david510 david510 is offline
View Beta Profile
At the Cliff
 
Join Date: Oct 2004
Location: India
Posts: 3,921
To redirect the 404 not found error to a image located in your web directory, use the following rule inside the .htaccess

ErrorDocument 404 /path/to/nopage_image.gif

__________________
David
www.cliffsupport.com
Affordable Server Management Solutions
FFmpeg Installation Service

Reply With Quote
  #3  
Old 12-21-2007, 12:35 AM
foobic foobic is offline
View Beta Profile
Community Guide
 
Join Date: Feb 2005
Location: Australia
Posts: 3,190
But of course that will redirect all missing pages to the same image. If you still want to use mod_rewrite, try this:
Code:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} \.(gif|jpg|jpeg|png)$ 
RewriteRule .* path/to/noimage.gif [L]
Using either method you must ensure that noimage.gif exists and is where you've told Apache to find it.

__________________
Chris <ClonePanel>
"Not everything that can be counted counts, and not everything that counts can be counted" - Albert Einstein

Reply With Quote
Sponsored Links
  #4  
Old 12-21-2007, 01:05 AM
EverSpeed EverSpeed is offline
View Beta Profile
Junior Guru Wannabe
 
Join Date: Feb 2004
Posts: 53
Yeah, I need it to replace only missing image files, since documents are already rerouted to a custom search page, so I do need to stick with mod_rewrite.

Is the path to noimage.gif relative to the site's public_html folder or to the server root?

Thanks!

Reply With Quote
  #5  
Old 12-21-2007, 01:09 AM
EverSpeed EverSpeed is offline
View Beta Profile
Junior Guru Wannabe
 
Join Date: Feb 2004
Posts: 53
Nevermind, I got it working on the first try by using the path relative to the site's folder. Perfect! This is going to save me so much time, not having to create individual "image not available" thumbnails.

Thanks!!!!!

Reply With Quote
  #6  
Old 12-21-2007, 01:09 AM
david510 david510 is offline
View Beta Profile
At the Cliff
 
Join Date: Oct 2004
Location: India
Posts: 3,921
If you are placing the rule inside the file .htaccess inside /home/username/public_html and the noimage.gif lies inside the same folder, you can specify the path as follows

RewriteRule .* /noimage.gif [L]

__________________
David
www.cliffsupport.com
Affordable Server Management Solutions
FFmpeg Installation Service

Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement: