It took me hours and hours of swearing at the screen before I finally found the solution for a problem I was facing with mod_rewrite. The situation was the following:
I was applying pretty urls to the website of a customer of mine using mod_rewrite. The rewrite rules were identical to the Zend htaccess file, which looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(js|ico|gif|jpg|png|css)$ /index.php
This ruleset basically says: Internally redirect every request to a URL that is
- not an existing file and
- not an existing directory and
- not a typical html asset
to index.php in the root of the website.
This htaccess file approach is also automatically in effect inside existing subdirectories but that’s usually easilly overcome by having a htaccess file in there that says:
RewriteEngine Off
But THAT is where things went wrong this time. When I requested a file in both the root directory as well as this subdirectory, I could not ever trigger a 404. Also not when requesting a non-existent .jpg file which, as you can see in the RewriteRule, should not trigger mod_rewrite.
After a lot of searching I found 1 website that mentioned the cause of my trouble. The server the website was hosted on was a DirectAdmin powered webserver that has custom error documents enabled which I deleted from the filesystem somewhere in the past.
What I needed to do is add the following line to the top of the htaccess file in the root of the website:
ErrorDocument 404 "The thing you are looking for does not exist on this planet... Go away."
This solved the whole problem. Of course the message can be whatever you deem fit for your visitors.
I figured out that I had similar trouble as mentioned on that other website by having the redirect target be some other url, followed by %{REQUEST_FILENAME} like so:
RewriteRule .* http://www.google.com/%{REQUEST_FILENAME} [NE,L]
Hope this helps.
Menno