2

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.

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

Comments

1

Use RewriteCond !-f along with a rewrite to the desired output page and a flag of R=404.

2 Comments

I see where you're going. The only issues is I don't want to redirect to a new URL I just want a 404 page at the requested url.
R with a code outside of 3XX will do only an internal redirect. Just point it to error404.js, or error.css, or whatnot.
0

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.

2 Comments

I was originally doing that. What I'm hoping to do is reduce the number of PHP requests to the sever.
AWinter: an ErrorDocument results in only a single PHP invocation anyways.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.