Using .htaccess for 404 pages?

I realized that my host’s default 404 pages were filled with spammy links, so I figured I should make up a custom 404 page for my websites.

I followed the directions from my host’s support pages, but I don’t have it quite right.

In my home folder, I have an .htaccess file with the following code:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /404.php

It works fine if someone goes to http://www.mysite.com/wrongpage, but if someone goes to http://www.mysite.com/subdirec…../wrongpage, all the styling of the 404 page is gone. Plus, none of the links work because it’s trying to go relative to the subdirectory, when it should be going relative to the site root…

What am I doing wrong?

I forgot the details about which was which. Something about PHP as an Apache module vs. PHP as CGI if I remember correctly. I think it was if PHP is an Apache module Apache passes of the request to the PHP engine as soon as it knows it’s PHP, so the rules never get applied.

Anyway, I’ve been using this for a while and it works for me, although it might not be the best. The “notfound” causes an error that doesn’t end with “.php” so Apache then uses the error document as specified.

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^.+\\.php$ /notfound [L]
ErrorDocument 404 /404.php