• 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

    • Yeah, I administrated a virtual desktop pool years ago during the Windows 7 days. There were a number of animations and effects that really killed performance. I setup a number of group policies to turn off all the things that make the desktop feel unresponsive. I didn't just blindly turn everything off, I turned things off that created a lot of motion, because that is where VDI struggles, but things that made the UI look pretty, but didn't use animation, I left turned on. Kind of a funny combination of settings to see Aero Glass and drop shadows enabled, but show window contents while dragging turned off, but hey, it is what worked.
    • It isn't fully a placebo. If a UI element uses a 0.25 second animation to appear, then removing the animation saves 0.25 seconds. That saved 0.25 seconds may not make any difference in how quickly you can click on something, but it will affect how quickly you can see it. Just depends on your goals.
    • I had to turn it off a long time ago on my work machine because virtual desktops were brutal when switching. Big thread about it on Microsoft forums. With animations off it's instant. Sucks as I had to uninstall Deskscapes as it requires animations but on my work machine productivity takes precedence.
    • Even though this tweak is basically just a placebo, I still find myself using it fairly often. There are better options out there, but the CrapFixer app https://github.com/builtbybel/CrapFixer is useful for testing and restoring the original state if needed
    • That’s such a heartwarming story — it really shows the strong sense of community Waffle House is known for. If anyone’s curious about what they offer or planning a visit, you can see full menu with updated prices and calories for 2025.
  • Recent Achievements

    • First Post
      Johnny Mrkvička earned a badge
      First Post
    • Week One Done
      viraltui earned a badge
      Week One Done
    • One Month Later
      serfegyed earned a badge
      One Month Later
    • Dedicated
      firey earned a badge
      Dedicated
    • Dedicated
      fettermanj earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      656
    2. 2
      ATLien_0
      224
    3. 3
      Michael Scrip
      224
    4. 4
      Xenon
      146
    5. 5
      +FloatingFatMan
      143
  • Tell a friend

    Love Neowin? Tell a friend!