404 Doesn't Work Within Virtual Directory

I hope I can explain this…

I’m using this code in my .htaccess file to hide .php extensions thereby making (for instance) cat.php just cat/ which I believe is called a virtual directory?

[php]RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.[^/])/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.
)$ /$1/ [R=301,L][/php]

That all works great. The problem is that if I try the error page as something like http://domainname.com/cat/somethingrandom/ it goes to the server 404 page instead of my custom 404 page. My custom 404 works everywhere else, including real directories. Help please?

Fixed using this instead:

[php]RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L][/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service