0

I need to send status 410 for missing images instead of 404, in Drupal 6. For example I have a image link as https://www.example.com/our-locations/directory/sub-directory/files/xyz.png which no longer exist in this location, then I need to send status 410 instead of 404.

Solutions already I have tried are:

RewriteCond %{REQUEST_URI} ^our-locations/directory/sub-directory/files/(\.jpg|\.png)$ [NC]
RewriteRule ^(.*)$ - [NC,R=410,L] 
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
TechValens
  • 437
  • 6
  • 20
  • REQUEST_URI string always starts with a leading slash. Change your pattern to this `^/our-locations/ directory/sub -directory/files/(\.jpg|\.png)$` – Amit Verma Feb 21 '18 at 14:44
  • Thank you @starkreen for your reply. I tried this as well, but it didn't worked for me. – TechValens Feb 22 '18 at 06:46
  • use hook_boot and test if file_exist , if not exit with header 410 – Fky Feb 22 '18 at 10:58

1 Answers1

0

You can use this :

 RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/path/.+\.(jpg|png|gif)$
RewriteRule ^ - [R=410,L]

The first condition RewriteCond %{REQUEST_FILENAME} !-f checks if the the file doesn't exist. Since you want to redirect only 404 images to 410 this condition prevents your other existing files from being redirected to 410. The next condition RewriteCond %{REQUEST_URI} ^/path/.+\.(jpg|png|gif)$ checks if the uri is /path/image.ext if both conditions are met then the RewriteRule is applied.

You can also use <filesMatch> and ErrorDocument directives to catch a 404 image file and then redirect it to a specific page but this will not return the correct error status. You can use RewriteRule instead .

<filesMatch "\.(jpg|png|gif)$">

ErrorDocument 404 http://exmaple.com/410.php

</FilesMatch>
Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • Thank you @starkeen, I tried your this solution. It is working fine if I using it in general (Normal PHP website without any CMS). But when we are trying to implement it in the Drupal 6, it is not working for us. We are using Drupal 6.20 with PHP v5.4.24. Please let me know if it is having any issue with the CMS .htaccess i.e., RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !=/favicon.ico RewriteRule ^(.*)$ index.php?q=$1 [L,QSA] – TechValens Feb 22 '18 at 06:58
  • @TechValens Put this at the top of your `drupal/htaccess` . – Amit Verma Feb 22 '18 at 15:05