Replacing Broken Images

Discussion in 'Apache' started by FeedBucket, Mar 14, 2006.

  1. #1
    Hi,

    I would like to have Apache serve up an alternate image if a requested image does not exist on the server. Is it possible to do this with .htaccess or the like?

    tia.
     
    FeedBucket, Mar 14, 2006 IP
  2. Slapyo

    Slapyo Well-Known Member

    Messages:
    266
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Try this. You can change the 404.php to whatever you have, but make sure you change it through out the entire .htaccess file.

    # 404 error handler
    ErrorDocument 404 /404.php
    
    RewriteEngine On
    # Is it the URI 404.php ? (ie was a 404 generated ?)
    RewriteCond %{REQUEST_URI} ^/404\.php$
    # Yes, so we extract the original URI from the http request (ie the first line)
    RewriteCond %{THE_REQUEST} ^GET[[:space:]](.+)[[:space:]]HTTP/ [NC]
    # Here we simply set an enviroment variable to be used later, this variable will contain the original REQUEST_URI
    RewriteRule ^.*$ - [E=ERROR404:%1]
    
    # Was the 404 URI ending with .jpeg .jpg .gif or .png? (ie was it an image?)
    RewriteCond %{ENV:ERROR404} \.(jpe?g|gif|png)$ [NC]
    # Yes, redirect internally to "your foo image''
    RewriteRule ^.*$ foo.gif [L]
    
    # Was the 404 URI ending with .html or .htm? (ie was it an html file ? )
    RewriteCond %{ENV:ERROR404} \.(html?)$
    # Yes, redirect internally to "your foo html file''
    RewriteRule ^.*$ foo.html [L]
    
    # All other 404 requests that do not match the extensions used are redirected to 404.php
    Code (markup):
     
    Slapyo, Mar 15, 2006 IP