Andrew Winter
Andrew Winter

Reputation: 1146

.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.

Upvotes: 2

Views: 2552

Answers (3)

Gumbo
Gumbo

Reputation: 655707

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.

Upvotes: 0

Andrew Winter
Andrew Winter

Reputation: 1146

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

Upvotes: 3

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799300

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

Upvotes: 1

Related Questions