Default Image if image doesn't exist with .htaccess

Discussion in 'Apache' started by nando, Apr 10, 2008.

  1. #1
    Like the title says, I'm looking for a rewrite rule that lets me redirect the url of a broken image to a default image.

    I saw this somewhere, but it didn't work...

    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
    RewriteCond %{REQUEST_URI} (.*).(gif|jpg|jpeg)$
    RewriteRule ^(.*) http://sc.msn.com/c/portal/logo/full/msft.gif
    Code (markup):
    Any and all help is appreciated
     
    nando, Apr 10, 2008 IP
  2. melon

    melon Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This one is working for me, just tested it:

    <IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} ^(.*)\.(png|gif|jpg|jpeg)$
      RewriteRule ^(.*) http://your.domain/path/to/image.png [L]
    </IfModule>
    Code (markup):
    Perhaps you forgot to prepend the the dot with a backslash.
     
    melon, Apr 10, 2008 IP
  3. nando

    nando Well-Known Member

    Messages:
    1,250
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    140
    #3
    Hey - thanks. That works for images on the main domain (example: domain.com/image.jpg) but not for images in other directories (example: domain.com/folder/image.jpg or domain.com/folder/folder2/image.jpg etc).

    Is there anyway to that? Thanks again and thanks in advance.
     
    nando, Apr 10, 2008 IP
  4. melon

    melon Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ok, my bad, change the second REQUEST_FILENAME to REQUEST_URI and it will work
     
    melon, Apr 10, 2008 IP
  5. hans

    hans Well-Known Member

    Messages:
    2,923
    Likes Received:
    126
    Best Answers:
    1
    Trophy Points:
    173
    #5
    there is a much easier method IF - and only IF you have all your pics in a subfolder of the domain - in folders without text files,
    then you can simply place IN the .htaccess OF THAT SUBfolder ( NOT domain / !!! ) another

    ErrorDocument 404 /path-to/replacement_pic.gif

    that is shorter than rewrite and does the same
    but for all - hence it only makes sense of NO text content files in that sub-folder structure.

    I have above working since long for wrong wallpaper requests
     
    hans, Apr 11, 2008 IP
  6. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #6
    Anyone ever tried placing an ErrorDocument directive isside a <FilesMatch> container restricted to images ?

    <FilesMatch "\.gif$">
       ErrorDocument 404 /path/to/image.gif
    </FilesMatch>
    Code (markup):
    I think a seprate container would be needed for each image type, I've had problems with redirecting to images of different types in the past with other applications.
     
    joebert, Apr 13, 2008 IP