• 0

.htaccess for missing images with wildcards


Question

I have a folder full of product images. And the image file name is something akin to:
 

/images/product_15_small.jpg
/images/product_15_large.jpg
/images/product_201_small.jpg
/images/product_201_large.jpg
/images/product_47502_small.jpg
/images/product_47502_large.jpg

 

If there is an error in the upload process and one of the files doesn't get created, say "/images/product_201_large.jpg", I'd like Apache to replace that file with this file: "/images/missing_0_large.jpg".  And in the case of the smaller image, I want it to be replaced with: "/images/missing_0_small.jpg".

 

I'm not sure how to do conditional RedirectMatch using HT access and a wildcard. Could someone guide me?  Here are the rules that I've tried to experiment with but none of them worked:

 

#TEST 1:
RedirectMatch 404 ^images/product_(\d+)_small.jpg	images/missing_(\d+)_small.jpg
RedirectMatch 404 ^images/product_(\d+)_large.jpg	images/missing_(\d+)_large.jpg


#TEST 2:
RewriteRule ^images/product_(\d+)_small.jpg		images/missing_(\d+)_small.jpg [L]
RewriteRule ^images/product_(\d+)_large.jpg		images/missing_(\d+)_large.jpg [L]

 

From the example above, "TEST 1" doesn't work however it seems to be the most logical format.

"TEST 2" does work, but it always redirects, and I want it to only redirect when the target file is missing (during a 404).

 

Please can an Apache expert help me?

6 answers to this question

Recommended Posts

  • 0

If you add

RewriteCond %{REQUEST_FILENAME} -f

above each of your RewriteRule lines from "Test 2", that should do what you're after. It's basically adding a condition to the rule, that makes the rule only apply if the file doesn't exist

  • 0

Thanks Dave,

So I added this to the .htaccess file:

 

<IfModule mod_rewrite.c>
  RewriteCond %{REQUEST_FILENAME} -f
  RewriteRule ^images/product_(\d+)_small.jpg	images/missing_0_small.jpg [L]
  RewriteRule ^images/product_(\d+)_large.jpg	images/missing_0_large.jpg [L]
 </IfModule>

 

But I still get a standard 404 error when I access files such as this missing image:

 

https://localhost/images/product_123_large.jpg

 

  • 0

You need the condition for each rewrite rule, otherwise it only applies to the first

 

  RewriteCond %{REQUEST_FILENAME} -f
  RewriteRule ^images/product_(\d+)_small.jpg	images/missing_0_small.jpg [L]
  RewriteCond %{REQUEST_FILENAME} -f
  RewriteRule ^images/product_(\d+)_large.jpg	images/missing_0_large.jpg [L]

 

  • 0

Ah, my mistake, try this

 

RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^images/product_(\d+)_small.jpg	images/missing_0_small.jpg [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^images/product_(\d+)_large.jpg	images/missing_0_large.jpg [L]

 

The ! makes the condition negative

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • A female Jem'Hadar makes no bloody sense whatsoever... They're a male only, genetically engineered, clone species...  Did the idiots making this not watch ANY of DS9, or bother to check a wiki, at ALL? This is going to be crap, isn't it?    
    • Internet Download Manager (IDM) 6.42 Build 42 by Razvan Serea Internet Download Manager (IDM) is a tool to increase download speeds by up to 5 times, resume and schedule downloads. Comprehensive error recovery and resume capability will restart broken or interrupted downloads due to lost connections, network problems, computer shutdowns, or unexpected power outages. IDM integrates seamlessly into Google Chrome, FireFox, Microsoft Edge, Opera, Safari, Internet Explorer, Maxthon and all other popular browsers to automatically handle your downloads. You can also drag and drop files, or use Internet Download Manager from command line. The program supports proxy servers, ftp and http protocols, firewalls, redirects, cookies, authorization, MP3 audio and video content processing. Changes in Internet Download Manager 6.42 Build 42: Updated Chrome extension to support Chrome manifest 3 Fixed bugs Download: Internet Download Manager 6.42 Build 42 | 11.7 MB (Shareware) Links: Internet Download Manager Website | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • I purchased, and that key is also not working. Well, it worked now.
    • Hello, In the past, I have purchased directly from Amazon, BestBuy, MicroCenter, Microsoft, and Newegg.  When purchasing, I have made sure the seller was either the store in question (not a third-party in their marketplace) or Microsoft. Regards, Aryeh Goretsky  
    • Right. And permission denied means it's still there, now just need to bypass authentication !!
  • Recent Achievements

    • One Month Later
      TheRingmaster earned a badge
      One Month Later
    • First Post
      smileyhead earned a badge
      First Post
    • One Month Later
      K V earned a badge
      One Month Later
    • Week One Done
      K V earned a badge
      Week One Done
    • Dedicated
      CarlosABC earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      639
    2. 2
      ATLien_0
      241
    3. 3
      Xenon
      175
    4. 4
      neufuse
      155
    5. 5
      +FloatingFatMan
      123
  • Tell a friend

    Love Neowin? Tell a friend!