Results 1 to 7 of 7
  1. #1

    .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

  2. #2
    Join Date
    Oct 2004
    Location
    Kerala, India
    Posts
    4,750
    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 sales AT cliffsupport DOT com
    CliffWebManager | Access WHM from iPhone and Android

  3. #3
    Join Date
    Feb 2005
    Location
    Australia
    Posts
    5,842
    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

    "Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them." - Laurence J. Peter

  4. #4
    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!

  5. #5
    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!!!!!

  6. #6
    Join Date
    Oct 2004
    Location
    Kerala, India
    Posts
    4,750
    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 sales AT cliffsupport DOT com
    CliffWebManager | Access WHM from iPhone and Android

  7. #7
    Join Date
    Jul 2006
    Posts
    166
    thanks for sharing this lovely tips, l was looking for a code for the missing images in wordpress.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •