• 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

    • Microsoft Edge comes pre-installed with Windows 11 and it's a great browser, so why not use it? Not everyone is trying to run away from Google, sometimes people just want convenience and less friction.
    • AI makes the games. AI plays the games We all watch them in a streaming service.... Welcome to the future.
    • No. The file manager is a lost cause. After all, its dev receives all the undeserved praise he could evre wish from Neowin, without having actually earned it. This has never lead to improvements.
    • TechPowerUp GPU-Z 2.70.0 by Razvan Serea GPU-Z is a lightweight system utility designed to provide vital information about your video card and graphics processor. At launch, it automatically scans your system and reports the card name, GPU, release date and transistors, BIOS version, ROPs, memory type, and memory size. Main Features: Supports NVIDIA, AMD, ATI and Intel graphics devices Displays adapter, GPU and display information Displays overclock, default clocks and 3D clocks (if available) Includes a GPU load test to verify PCI-Express lane configuration Validation of results GPU-Z can create a backup of your graphics card BIOS No installation required, optional installer is available Support for Windows XP / Vista / Windows 7 / Windows 8 / Windows 10 (both 32 and 64 bit versions are supported) GPU-Z 2.70.0 changelog: Improved kernel driver security Added die size for Qualcomm Adreno 741 Added support for NVIDIA RTX 6000D, RTX Pro 500 Blackwell Embedded, Tesla V100-DGXS-32GB, PG500-216 Added support for Intel Arc Pro B70, B65, A60 ES, Alder Lake ES Added support for Qualcomm Snapdragon X2 Elite, 778G/782G Added vendor detection for HKC/Sambada, AWES Download page: GPU-Z 2.70.0 | 11.1 MB (Freeware) View: GPU-Z Website | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • I know I won't ever be using it to make my game. I'd rather pay humans.
  • Recent Achievements

    • One Month Later
      Vincian earned a badge
      One Month Later
    • First Post
      Jocimo earned a badge
      First Post
    • Week One Done
      suprememobiles48 earned a badge
      Week One Done
    • One Month Later
      Windows Guy earned a badge
      One Month Later
    • One Month Later
      Prasann earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      518
    2. 2
      +Edouard
      159
    3. 3
      PsYcHoKiLLa
      86
    4. 4
      Steven P.
      67
    5. 5
      neufuse
      64
  • Tell a friend

    Love Neowin? Tell a friend!