Broken Image Replacement ModRewrite

Discussion in 'Apache' started by jarus323, Feb 1, 2010.

  1. #1
    Hey guys, I've been reading on and on about how to fix broken image link. I can't figure out what I am doing wrong.
    Here's what I have:
    RewriteCond "%{REQUEST_URI}" !-f
    RewriteRule \.(gif|jpe?g|png|bmp) /images/nophoto.gif [NC,L]

    my default image is at http://www.mydomainname.com/store/images/nophoto.gif
    I want all broken images from any url from any product on my site to point to the one above.

    What am I missing? Any ideas?
     
    jarus323, Feb 1, 2010 IP
  2. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #2
    %{REQUEST_URI} generally doesn't include the full path to the file. You'll need to prepend the path to the directory where the request URI would reside for the -f flag to work. Chances are the %{DOCUMENT_ROOT} environment variable with work for that purpose.

    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
    Code (markup):
    Now, I don't remember which browsers it was, or whether it was versions that people don't use anymore, but it used to be that rewriting the URI for an image to a different type of image, for instance the request is for a JPG and you want to return a default GIF, the browser will be expecting a JPG and will consider the GIF a broken file before just displaying a broken image icon.

    You'll want to at least make sure you use the [R] flag so that the browser has a chance to react to the image change. Ideally you'll rewrite each image type to the type requested, meaning you'll have multiple defaults in different formats. But, as I mentioned, things may have changed so you'll want to experiment with that for the browsers your audience uses. :)
     
    joebert, Feb 2, 2010 IP
  3. jarus323

    jarus323 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the tip. Most of my images are JPG so I rewrote the short script:
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
    RewriteRule \.(jpg) /images/nophoto.jpg [NC,L]

    I'm mainly consigned about the "bastard browser from hell" IE. Any other tips to why wouldn't this work?
     
    jarus323, Feb 3, 2010 IP
  4. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #4
    Is this a server you can modify the virtual-host or server-config on, in order to use rewritelog ?
     
    joebert, Feb 4, 2010 IP
  5. jarus323

    jarus323 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I'd say virtual-host. It is hosted by hostmonster. The root of my directory has an .htaccess file. I can provide screen shots if necessary.
    Thanks for your help by the way. I have posted this question on numerous forums but nobody seems to know. I wonder if this is very hard to do or something.
     
    jarus323, Feb 4, 2010 IP
  6. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #6
    I don't think you're going to have access to RewriteLog at Hostmonster.

    I would start by trying this in the htaccess of the sites root directory.
    RewriteEngine On
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
    RewriteRule \.(jpe?g|gif|png)$ http://www.mydomainname.com/store/images/nophoto.gif [L,R=301]
    Code (markup):
     
    joebert, Feb 6, 2010 IP