• 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

    • I commented on ONE thing, the rest is just an explanation for the unintroduced. The distinction between Skype and SfB was not cloud vs on-prem, period. That's all I refuted.
    • "that supports both AMD and Intel" Why even say such a thing? That's like saying an HDMI cable that supports both LG and Dell displays or something.
    • since when was a xx50 series GPU "good" this is another one of those "duh" what did you expect, kinda reviews.
    • Giga Computing launches liquid- and air‑cooled AI servers built on NVIDIA HGX B200 platform by Fiza Ali Giga Computing has today expanded its enterprise‑grade GPU server portfolio with four new systems built upon NVIDIA’s HGX B200 platform. These servers combine advanced cooling options with support for Intel’s latest AI‑optimised Xeon 6 processors. According to NVIDIA, HGX B200, powered by the company's Blackwell architecture, delivers up to 15 times faster real‑time inference, where the trained AI model makes predictions or decisions based on new data, on trillion‑parameter models compared to the previous generations. The 4U GIGABYTE G4L3 Series features separate CPU and GPU chambers and employs an advanced direct liquid‑cooling (DLC) system. According to the tech giant, this approach not only sustains peak performance during demanding AI training and inference tasks but it also drives down both power consumption and data‑centre cooling costs. The two variants which are currently available are the G4L3‑SD1‑LAX5, which supports the latest Intel Xeon processors, and the G4L3‑ZD1‑LAX5, which supports AMD EPYC CPUs. On the other hand, the GIGABYTE G894 Series uses optimised airflow and works with a variety of x86 platforms and baseboard GPU setups, including NVIDIA’s HGX B200. It comes in two models: the G894‑AD1‑AAX5, which supports Intel Xeon 6900‑Series processors such as the 6962P, and the G894‑SD1‑AAX5, which supports Intel Xeon 6700‑ and 6500‑Series processors, including the 6776P. GIGAPOD, Giga Computing’s AI supercomputing solution, has also been updated to include the new NVIDIA HGX B200 platforms. Built for the rigours of training large language models and generative AI, it promises improved throughput, higher energy efficiency, and a range of cooling options to accommodate advancing AI demands. With the addition of these HGX B200-powered systems to its lineup, Giga Computing is expanding its server offerings to better support demanding AI workloads. The focus on efficient cooling, compute density, and compatibility with the latest processors positions these new models as options for enterprises scaling up AI infrastructure.
    • OpenHashTab 3.1.1 by Razvan Serea OpenHashTab is a convenient shell extension that allows users to easily calculate and verify file hashes using file properties. It offers support for 28 different algorithms, ensuring compatibility with a wide range of hash types. With its high-performance hash calculation capabilities, OpenHashTab delivers efficient results. The extension seamlessly integrates with the Windows operating system, providing a native look and feel. It also offers support for high DPI screens and long paths. Additional features include multilingual support, the ability to check hashes against VirusTotal, hash checking against checksum files, hash export to file or clipboard, and optional context menu options for faster access. OpenHashTab supports file associations and can be used in standalone mode. OpenHashTab is compatible with Windows 7 or newer versions, supporting x86, x64, and ARM64 architectures. Usage Most of the actions should be obvious. Some not-so-obvious features are listed here: You can select multiple files or folders, all files will be hashed, directories traversed Double click hash to copy it Double click name or algorithm to copy the line in sumfile format Right click for popup menu: copy hash, copy filename, copy line, copy everything The counters next to the status text is in the format (match/mismatch/nothing to check against/error) Columns sort lexicographically, except the hash column which sorts by match type Selecting the tab on a sumfile will interpret it as such and hash the files listed in it. If a hashed file has a sumfile with same filename plus one of the recognized sumfile extensions and the option for it is enabled, the file hash is checked against it. Algorithms CRC32, CRC64 (xz) xxHash (XXH32, XXH64) xxHash3 (64 and 128 bit variants) MD4, MD5 RipeMD160 Blake2sp SHA-1 SHA-2 (SHA-224, SHA-256, SHA-384, SHA-512) SHA-3 (SHA3-224, SHA3-256, SHA3-384, SHA3-512) BLAKE3 (256 bit, 512 bit) KangarooTwelve (264 bit, 256 bit, 512 bit) ParallelHash128 (264 bit) and ParallelHash256 (528 bit) Streebog (GOST R 34.11-12) (256 bit, 512 bit) OpenHashTab 3.1.1 changelog: This release fixes a build problem with 3.1.0 causing StandaloneStub to be unable to start. Reduce the size of AlgorithmsDll Move installer to WiX, overall register components better Build x86 so that 32-bit explorer replacements can also have the extension Reintroduce x86 version since we're building it anyway Translation updates Download: OpenHashTab 64-bit | OpenHashTab 32-bit | ~8.0 MB (Open Source) Download: OpenHashTab ARM64 | 8.3 MB View: OpenHashTab Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Week One Done
      956400 earned a badge
      Week One Done
    • First Post
      loose_observer earned a badge
      First Post
    • Week One Done
      BeeJay_Balu earned a badge
      Week One Done
    • Week One Done
      filminutz earned a badge
      Week One Done
    • Reacting Well
      SteveJaye earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      446
    2. 2
      ATLien_0
      157
    3. 3
      +FloatingFatMan
      148
    4. 4
      Nick H.
      65
    5. 5
      +thexfile
      62
  • Tell a friend

    Love Neowin? Tell a friend!