htaccess Redirect when 404 - .htaccess

I have a subdirectory called /marker, in it are some png files. Now I want the file /marker/standard.png to be returned whenever an attempt is made to call a png that does not exist. In other words: If, for example, /marker/doesntexist.png is called (which doesn't exist), /marker/standard.png should be delivered.
Sounds quite simple, but several attempts with RewriteRule or ErrorDocument failed.
Any ideas?
tia

You can do the following to serve /marker/standard.png if another .png file in the /marker directory is requested that does not exist.
This uses mod_rewrite and should go near the top of the .htaccess file in the document root.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^marker/(?!standard\.png$).+\.png$ marker/standard.png [L]
I took the extra step to exclude requests for standard.png itself (using a negative lookahead), in case this doesn't exist for some reason.
Note that this is a case-sensitive string comparison. eg. Only .png files are matched, not .PNG.
This naturally results in a 200 OK response when standard.png is served. If you still need a 404 Not Found status then use ErrorDocument instead (see the next section).
Using ErrorDocument
You also mentioned the use of an ErrorDocument directive. This is another solution, although it will result in a 404 HTTP response status, which may or may not be desirable.
For example, in order to restrict this ErrorDocument just to the /marker subdirectory then create an additional .htaccess file in this subdirectory with the following:
ErrorDocument 404 /marker/standard.png
This will naturally serve standard.png for any non-existent request to the /marker subdirectory. To restrict this to .png requests only then wrap the ErrorDocument in an <If> expression. For example:
<If "%{REQUEST_URI} =~ /\.png$/">
ErrorDocument 404 /marker/standard.png
</If>

Related

throw 404 error with .htaccess file and php

I've got url like www.web.com/home and www.web.com/about with .htaccess file:
RewriteEngine On
Options +FollowSymLinks
ErrorDocument 404 http://www.web.com/page-not-found.html
RewriteRule "home" "index.php?c=home&m=main"
RewriteRule "about" "index.php?c=home&m=about"
If I type something like www.web.com/asd, .htaccess will throw 404 error and direct page-not-found.html file
But, If I type www.web.com/homesss or www.web.com/about/a/b/c, the .htaccess will not throw the 404 error
How do I fix that? I use pure PHP
Use meta characters in rewrite rule to 1:1 match:
^ --- Matches the beginning of the input
$ --- Matches the end of the input
[L] --- Flag last to stop processing rule after match
RewriteEngine On
Options +FollowSymLinks
ErrorDocument 404 http://www.web.com/page-not-found.html
RewriteRule ^home$ index.php?c=home&m=main [L]
RewriteRule ^about$ index.php?c=home&m=about [L]
In addition to #JarekBaran's answer...
If I type something like www.web.com/asd, .htaccess will throw 404 error
Not with the code you've posted...
ErrorDocument 404 http://www.web.com/page-not-found.html
When a request does not map to a resource, the above triggers a 302 (temporary) redirect to the http://www.web.com/page-not-found.html. There is no 404 HTTP response. Details of the request that triggered response are lost. /page-not-found.html is exposed to the end user. This is generally bad for SEO and delivers a bad user experience.
The 2nd argument to the ErrorDocument directive should be a root-relative URL-path, starting with a slash so that the error document is called with an internal subrequest. Apache then responds with a 404 status. The error document is not exposed to the end user.
(Very rarely should an absolute URL be used here.)
For example, it should be like this instead:
ErrorDocument 404 /page-not-found.html
Reference:
https://httpd.apache.org/docs/2.4/mod/core.html#errordocument

URL rewriting for nested level directories

I have a assets folder in my webserver which contain files like javascript, css and deep level of other folders. When user tries to access this folder using url like:
www.example.com/assets
I show then 404.html file, which is done easily as:
RewriteEngine On
RewriteRule ^/assets/$ /404.html [NC,L]
But as I say it a multiple folder inside it, also for which show 404 error. I don't want to write rewrite rule for all the folders inside it (because their is many). I am a beginner in url rewriting, can anybody show me how can I achieve this with a line or two lines of code.
Add the line:
RewriteRule ^/assets/(([^/]*)/)+$ /404.html
Which will match /assets/{anything}/{anything}/{anything}/...
If you're wanting to prevent visitors from listing the files in your directories, you can use
Options -Indexes
in your .htaccess file.
You just need this rule to forward all /assets/* requests to /404.html:
RewriteRule ^assets(/|$) /404.html [NC,L]
Make sure this rule is your first rule.

redirect 403 error using .htaccess

I want to redirect any 403 using .htaccess, but it does not seem to work.
Options +FollowSymlinks
RewriteEngine on
ErrorDocument 403 notfound.html
RewriteRule notfound.html
All help appreciated.
Thanks
Jean
The URL part of an ErrorDocument directive should either start with a leading slash, which indicates a path that's relative to your DocumentRoot, or it should be a full URL (if you want to use an external document).
You shouldn't need the RewriteEngine and RewriteRule directives at all here.
So, assuming your notfound.html is at the root level of your site, your directive should look like:
ErrorDocument 403 /notfound.html
I found a problem with the earlier example, which was:
ErrorDocument 403 /notfound.html
I have a directory on my site storing images, say at domain.com/images
I wanted that if someone tries accessing the directory's URL that they be redirected to the site's homepage. My problem was, with the above, that the URL in the browser would remain domain.com/images and then the homepage would load, but the homepage would reference stylesheets that it could not access from that URL. (And it makes sense - 403 is supposed to show an error message).
I then tried researching how to redirect a 403 error to another URL and a few sites said you can't do a 302 or 301 - you're missing the point: 403 is an error, not a redirect.
But I found a way. This is the .htaccess file inside the /images directory:
Options -Indexes
ErrorDocument 403 /images/403.php
RewriteEngine on
RewriteRule ^403.php$ /index.php [R=301]
So basically the first line prevents directory listing.
The second line says that for "permission denied" (ie. 403 error, which is what the first line gives), the site should load the file "/images/403.php". But you don't actually need to create any 403.php file in that directory, because...
The third line turns on your redirect engine
And the fourth line says that if the user attempts to load the page 403.php (ie. in this "/images" directory where this .htaccess file is located) then they should be redirected, using a 301 (permanent) redirect, to /index.php - which is the index of the root directory (ie the homepage), and not the index of the /images subdirectory (and indeed there is no index in "/images" anyway).
Hence - how to get a 403 to redirect to another URL using a 301 (permanent) redirect (or, you could try a 302 temporary redirect too).
Here is a super easy way to do this:
Use this line
Options -Indexes
ErrorDocument 403 /goaway.html
where goaway.html contains the following line in the head section:
<meta HTTP-EQUIV="REFRESH" content="0; url=HERE YOU PUT THE URL YOU ARE REDIRECTING TO" />
And that's it... each 403 error will go to your goaway.html file in root directory and goaway.html will redirect them to the URL of your choice
RewriteEngine On
ErrorDocument 403 http://www.yourdomain.example/

.htaccess and custom ErrorDocument for different file types

I'm wondering if it's possible to use mod rewrite along with the ErrorDocument deceleration to customize the error pages depending on what type of file is requested.
For example a non-existent html or php file is requested Apache will give nice custom HTML page.
But if a non-existent image, js, css, etc... file is requested then Apache will serve out a basic html file with only a link on it.
A good example of this behavior is Facebook. Try requesting a bogus JavaScript file, you will receive a different page then if you were to request a non-existent php file.
Ended up using a combination of both ErrorDocument and RewriteRules this works because the php page throws a 404 Not Found for me.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*(?<!.js|.css|.ico|.txt|.bmp|.gif|.png|.jpeg|.jpg)$ /error/404.php [L]
ErrorDocument 404 /404_basic.html
Use RewriteCond !-f along with a rewrite to the desired output page and a flag of R=404.
You could use a PHP script for your 404 page:
ErrorDocument 404 /error404.php
There you can analyze the URL path with PHP (see $_SERVER['REQUEST_URI']) and determine what kind of resource has been requested or is expected.

How can I get non existant files mapped correctly in .htaccess?

Duplicate:
How to rewrite non existant files to
‘default’ files?
(.htaccess)
How would I "rewrite" to a location if a file doesn't exist? I don't want to use a 404 redirect, but an actual rewrite.
So for example, let's say it is a directory with images. If the image isn't found, then it rewrites to a default image?
I.e.,
images/1.jpg
images/2.jpg
images/default.jpg
if someone tried to access "website.com/images/3.jpg",
since that doesn't exist, I want it to go to:
"website.com/images/default.jpg"
This was a previous "posted" solution, but didn't quite work:
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule /images/.* /images/error.jpg [L]
It still doesn't "get" the right image (just goes as a regular 404 request).
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^images/.* /images/error.jpg [L]
Obviously this only redirects if missing file is under /images/... but you can easily modify it for your own needs
Well, your previous posted solution is on the right track, but there's some slight craziness with it. Try this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule images/.* /images/default.jpg [L]
You should better send a 404 status code if the file really doesn’t exist rather than just a substitute with a status code other than 404. Otherwise the URL will be handled as valid.
So in your case I recommend you to set the ErrorDocument of the images directory to your default image:
<Directory "/path/to/your/images/">
ErrorDocument 404 /images/default.jpg
</Directory>
But note that the <Directory> block is only available in the server or virtual host configuration context and not the per-directory context (thus .htaccess).
If you cannot use the above, you could use a custom script as your custom error document to check what URL has been requested (see Request_URI environment variable) and send the default image if necessary. The ErrorDocument directive then should look like this:
ErrorDocument 404 /your-error-404.script
re-write your 404 document for your images folder:
(In your .htaccess file in your images folder)
ErrorDocument 404 default.jpg

Resources