Add status code 410 for missing images in Drupal 6 - php

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]

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>

Related

500 Server error instead of 404 on nested urls due to mod rewrite htaccess rule(s)

I have a site with custom .htaccess file that handles few things:
1) It treats urls without ".php" extensions as if it has ".php" in the end.
2) It redirects http:// and http://www. urls to https://www.
Here is the .htaccess file:
ErrorDocument 404 /404.php
Options -MultiViews
RewriteEngine On
## add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
# if not a directory and .php file is present then add .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Everything works as expected, but I have observed some strange behavior that resulted in 500 errors instead of 404:
1) When you visit non-existed root level url such as https://www.example.com/skjdfhkj it redirects to 404 as expected.
2) When you visit non-existed nested url such as https://www.example.com/some-text/skjdfhkj where some-text does not match any existing php files, then it returns 404 as expected.
3) However, when you visit some non-existed nested url such as https://www.example.com/some-existing-page-name/skjdfhkj , where some-existing-page-name matches the name of existing php file (https://www.example.com/some-existing-page-name.php), then it gives out a 500 Server Error.
My question is: how do I update my htaccess to properly return a 404 instead of 500 when someone visits non-existing nested url such as https://www.example.com/some-existing-page-name/skjdfhkj (where some-existing-page-name matches the name of existing php file (https://www.example.com/some-existing-page-name.php)) ?
I guess it has something to do with mod rewrite that treats urls without .php extensions as if it had .php, but don't know how to modify htaccess to make it work properly :(
Try changing last rule as this:
# if not a directory and .php file is present then add .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
%{REQUEST_FILENAME} sometimes can give unexpected matches from filesystem using partial matches.

missing directories don't trigger Error Document Handling htaccess

Basically, I've got the following .htaccess file:
Options -Indexes
ErrorDocument 400 /index.php?error=400
ErrorDocument 401 /index.php?error=400
ErrorDocument 403 /index.php?error=400
ErrorDocument 404 /index.php?error=400
ErrorDocument 500 /index.php?error=400
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Enable sites without extentions
options +MultiViews
<IfModule mod_rewrite.c>
Options +FollowSymlinks
# Adaptive-Images -----------------------------------------------------------------------------------
# Add any directories you wish to omit from the Adaptive-Images process on a new line, as follows:
# RewriteCond %{REQUEST_URI} !ignore-this-directory
# RewriteCond %{REQUEST_URI} !and-ignore-this-directory-too
#RewriteCond %{REQUEST_URI} !img
# don't apply the AI behaviour to images inside AI's cache folder:
#RewriteCond %{REQUEST_URI} !ai-cache
# Send any GIF, JPG, or PNG request that IS NOT stored inside one of the above directories
# to adaptive-images.php so we can select appropriately sized versions
# RewriteRule \.(?:jpe?g|gif|png)$ adaptive-images.php
# END Adaptive-Images -------------------------------------------------------------------------------
# SEO Friendly Redirect Rules -----------------------------------------------------------------------
RewriteRule ^(admin)/?$ admin/ [L,NC]
RewriteRule ^([A-Za-z0-9-]+)/?$ /index.php?title=$1 [L]
RewriteRule ^([A-Za-z0-9_]+)/([A-Za-z0-9_]*)/?$ index.php?title=$1&ch=$2 [L]
RewriteRule /$ index.php [END]
As of right now, the rules and conditions all work great except for one thing. If I navigate to say: "example.com/nonexistingfolder" , it is using this rule to process:
RewriteRule ^([A-Za-z0-9-]+)/?$ /index.php?title=$1 [L]
Which brings up a php error because the argument passed is invalid. Which is to be expected, because it doesn't exist. But if I no navigate to: "example.com/nonexistingfolder/index.php" , I'm correctly greeted with my 404 page defined at the top. (don't worry about the slashes, in the original these are permalinks)
ErrorDocument 404 /index.php?error=400
I don't know too much outside of what I've learned today about .htaccess and mod_rewrite. Is there some other way I should be going about this? Or am I missing other statements?
I've tried adding the following lines to my .htaccess with no progress:
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
Either these don't help in my case (and I thought the second would for sure), or I am still doing something wrong.
I expected the code to be rather straightforward, however this has left me scratching my head for a couple of hours. Thanks for taking the time to read and ponder this with me! Have a good one.
EDIT: I added these three lines of code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? /index.php?error=400 [L]
Which works when I navigate to something that doesn't exist. Yet, it interrupts the other RewriteRule I have set: RewriteRule ^([A-Za-z0-9-]+)/?$ /index.php?title=$1 [L]
So now I am for sure at a loss at what to do.
To further exemplify:
Say I navigate to "website.com/nonexistingfolder". Which now because I added the three lines above, shows the 404 page correctly. However, because of the single RewriteRule above, if I navigate to: "website.com/indexquery" it will also show the 404. How can I tell my .htaccess to differentiate between the two?

Useragent redirect if 404

At the moment if a 404 page is requested by a useragent it returns "Unable to get URl"
What I'm looking for is a script (probably best done in php or .htaccess) that will redirect to a certain page if called from a certain useragent if it's a 404 page.
I think it could also be done with a status type thing as if it's a 404 page it returns Status 404 in the visitor log.
At the moment I use this to simply redirect if it's the useragent.
RewriteCond %{HTTP_USER_AGENT} WinInet
RewriteRule ^index.html$ /pagehere.html [NC,L]
In your .htaccess add this code
ErrorDocument 404 /var/www/website/redirect_404.php
#(Your 404 error redirect file location)
#ErrorDocument 404 http://example.com/redirect_404.html (To redirect to a url)
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/redirect_404/$
RewriteRule ^(.*)$ /var/www/website/redirect_404.php [L]
#The ErrorDocument redirects all 404s to a specific URL
Inn your redirect_404.php file enter this code
<?php
header('Location: /errorfolder/404/'); //Path to your error file
exit;
?>
You can use this rule for non-files and non-directories (similar to 404):
RewriteCond %{HTTP_USER_AGENT} WinInet
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . /pagehere.html [L]

How do I target multiple directories with RewriteCond in the htaccess file?

I have .htaccess file which I am using to remove .php extensions and apply some 'vanity' to a folder. I am also trying to add custom error pages.
I am not very familer with htaccess at all and have only ever used it to redirect a user.
I have this so far
RewriteEngine ON
# hide php extentions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC]
#vanity on a directory
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://localhost/ownitall/profile.php?user=$1 [NC]
#Error Handling
ErrorDocument 403 /errors/403.php
ErrorDocument 404 /errors/404.php
ErrorDocument 500 /errors/500.php
There are two main probelms with this:
1: The error handling does not work at all, if a page doesn't exist anyway I just redirected to profile.php.
RewriteRule ^(.*)$ http://localhost/ownitall/profile.php?user=$1 [NC]
So far example if i was to go to page /about-us/sub-folder/ - which doesn't exist I would get redirected to profile.php.
* EDITED *
//PROFILE.php document
<?php if(isset($_GET)){
print_r($_GET);
}?>
To further exlpain - If i went to page '/about/index' in the site I get this output, which comes from profle.php
Array
(
[user] => about/index.php/index
)
Instead of my error handling document.
* EDITED *
That same line is also being applied to any sub folders in the root directory, is there a way I can target that to apply to that folder and not any sub-folders with that directory or should I handle this in the profile.php document then redirect the user to the relevant error document if the page doesn't exist on the site.

.htaccess redirect to requested filename with pretty URL?

I want to redirect to the requested file name so if someone types
www.example.com/user/1
it should redirect to the user.php file with 1 lets so someone type
www.example.com/apple/3 or www.example.com/apple which does not exists but it should redirect still redirect to apple which will auto give 404
I have no idea on how to do this i found this code here it works but redirects everything to index.php even if i type apple
ErrorDocument 404 /404.php
RewriteEngine On
RewriteBase /SimpleMVCTest/
#Make sure it's not an actual file
RewriteCond %{REQUEST_FILENAME} !-f
#Make sure its not a directory
RewriteCond %{REQUEST_FILENAME} !-d
#Rewrite the request to index.php
RewriteRule ^(.*)$ index.php?get=$1 [L]
Edit:
Little more detail
can RewriteRule ^(.*)$ index.php?get=$1 [L] be dynamic so instead of index.php it gets the requested one from the url?
so if a user types www.example.com/{requested URL?something=something}
then i want .htaccess to redirect to {requested URL/something/something} and all to be dynamic to if a user types cars or pear or rabbit/3 it would redirect to that page without it being hard coded in htaccess like rabbit.php?someting=someting i want every thing to be dynamically so something like this RewriteRule ^(.*)$ {requested URL?something=something}?get=$1 [L]
Edit 2:
OK after a lot Google/Youtube/Stackoverflow research and putting random things together I finally got something that partly works here:
ErrorDocument 404 /404.php
Options -MultiViews
RewriteEngine On
RewriteBase /SimpleMVCTest/
#Make sure it's not an actual file
RewriteCond %{REQUEST_FILENAME} !-f
#Make sure its not a directory
RewriteCond %{REQUEST_FILENAME} !-d
#Rewrite the request to REQUESTED_FILENAME
RewriteRule ^(.*)$ $1.php?get=$1 [L]
There is two issues I have noticed with this
1: Giving www.example.com/car/1 will result in 500 Internal Error but if i write www.example.com/car it works fine. How do i make it so i can go car/1 and it will not give 500 internal Error
2: if i write www.example.com/somethingthatdoesnotexist or www.example.com/somethingthatdoesnotexist/1 it gives 500 internal but should just redirect to 404 but it's not found
How would I do this?
In a .htaccess file use the following:
Options -MultiViews
RewriteEngine On
RewriteRule ^user$ user.php
RewriteRule ^user/$ user.php
RewriteRule ^user/([a-zA-Z0-9\-\_]+)$ user.php?user=$1
If you use as a link now something like profile/username it will show that users profile.
It may mess up some css an js files, if it does on the page in the head use:
<base href="/" />
This should correct any css/js/image links

Categories