When I try to remove my PHP extensions from all my files using my .htaccess file on my Apache server, everything works great. The extensions are removed, and everything looks much better.
However, I’m having one small issue: when I would normally go to a page such as ./nonexistent.php, I would get a 404 error. But, when I rewrite my URLs, and I go to ./nonexistent, I instead get a 500 Internal Server Error.
Ideally, I would like to simply redirect my user to a custom ‘Page Not Found’ page, but I can’t currently find a way to do this.
Here’s the code I’m using:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php
I’ve tried setting: ErrorDocument 500 /nope, however, this doesn’t seem to have an effect either.
So, to conclude, does anyone know how to rewrite extensions, while maintaining the same functioning of the ‘Page Not Found’ system that is the default?
When you are requesting a non-existent file using the above rewrite conditions, you are running into an infinite redirect.
If you access
http://yoursite.com/i-dont-exist, the first condition is evaluating true,i-dont-existis a non existent file, so it will try to rewrite toi-dont-exist.phpwhich also doesn’t exist so the rewrite pattern continues until Apache limits the recursion and gives you a 500 error (essentially it is continually rewritten toi-dont-exist.php.php.php.php.php...phpuntil you encounter the 500 error.You can resolve this by adding an additional check to make sure the file with the
.phpextension exists before rewriting.If the
file.phpexists, then it will rewrite to it, otherwise it will not, and the 404 error page will be served.