.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 th开发者_高级运维en 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.
精彩评论