We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23491 ☆ A M B ☆
    • 1,056 Posts
    It dawned on me today that I often use my own personal server for hosting a wide array of image types: screen shots, site/layout images, buttons, affiliate banners, digital camera photos, etc

    Specifically I frequently upload screen shots for bugs and error troubleshooting, etc. and often link directly to the image within forum posts, emails, or blog entries.

    So what if I wanted to delete or move the image from the defined location on my server?

    Well, since I am running MODx, the file ultimately will not be found and MODx will handle this by serving my custom 404 Error page.
    For most, this action is probably desired. If someone is trying to hit my site and the requested URL does not exist, I’d want them to at least access the portion of my site that does exist.

    However, this also means that for every user that is viewing the forum post with the old image reference that my site would handle each request via MODx. This sends a few unnecessary calls to my database, however. The seemingly empty forum post does not benefit either.

    Until now.

    A little .htaccess magic allows me to serve an "Image Not Found" image whenever an old image reference is requested, bypassing the database entirely:

    # Rewrite old image references to an "image not found" image (Prevent calls to the database)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} \.(png|jpg|jpeg|gif|bmp|tif|tiff)$ [NC]
    RewriteRule ^(.*)$ assets/images/image_not_found.jpg [L]
    


    If using Friendly URLs, be sure to place the previous code ABOVE this section in your .htaccess file.
    # The Friendly URLs part
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
    


    The result:
    # Rewrite old image references to an "image not found" image (Prevent calls to the database)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} \.(png|jpg|jpeg|gif|bmp|tif|tiff)$ [NC]
    RewriteRule ^(.*)$ assets/images/image_not_found.jpg [L]
    
    # The Friendly URLs part
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
    


    8) Notice how you can handle for various image types: PNG, JPG, JPEG, GIF, BMP, TIF, TIFF, etc

    (Don’t forget to create your image_not_found.jpg image (otherwise, I’d recommend pointing to assets/js/x.gif)! You could even place your URL on the image so those interested can find your site!)
      Mike Reid - www.pixelchutes.com
      MODx Ambassador / Contributor
      [Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
      ________________________________
      Where every pixel matters.
      • 22303 MODX Staff
      • 10,725 Posts
      Great tip.

      FWIW, there are other file-types that can be handled in a similar way to further prevent bootstrapping the MODx request handler on bad requests to css files, js files, or just about any other file-type that MODx shouldn’t be handling for you. This can help improve performance on high-traffic sites, and can be especially useful if you have hackers (or security auditors) constantly searching for vulnerabilities. wink

      See some similar discussions at Drupal for more background on this kind of issue...
        • 23491 ☆ A M B ☆
        • 1,056 Posts
        Quote from: OpenGeek at Aug 10, 2007, 06:16 PM

        ...further prevent bootstrapping the MODx request handler on bad requests to css files, js files, or just about any other file-type that MODx shouldn’t be handling for you...

        Exactly!

        Also, this can be used as a good troubleshooter for within your own site during instances where you may have mistyped an image name/extension, etc.

        E.g.
        You pull up your site expecting to see your logo, but you’d see an "Image Not Found" image instead wink
          Mike Reid - www.pixelchutes.com
          MODx Ambassador / Contributor
          [Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
          ________________________________
          Where every pixel matters.
          • 6726
          • 7,075 Posts
          Missed that one, great thanks !
            .: COO - Commerce Guys - Community Driven Innovation :.


            MODx est l'outil id
            • 23491 ☆ A M B ☆
            • 1,056 Posts
            Quote from: davidm at Aug 23, 2007, 04:57 AM

            Missed that one, great thanks !

            I have found that, if you don’t want to create an "image not found" image, simply redirect to assets/js/x.gif smiley
            This way, the request still bypasses the DB, but there is no obvious missing/out-of-place image.

              Mike Reid - www.pixelchutes.com
              MODx Ambassador / Contributor
              [Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
              ________________________________
              Where every pixel matters.