"File not found." instead of custom 404 file with php-fpm enabled
-
*eye roll*
Thanks Rex!
0 -
Since this is the year to respond to 6 y/o posts... lol I realized I never posted my very simple solution to the problem of PHP-FPM ignoring .htaccess ErrroDocument statemetns when the problem is a missing .php file.
Typically there would be something link this in .htaccess:
#error pages
ErrorDocument 404 /404.php
ErrorDocument 403 /403.phpThis is ignored when PHP-FPM in enabled. (The workarounds discuss at the top part of this thread cause other issues, as documented.)
Placing this in .htaccess serves to send traffic to a bad .php file name to the 404 handler of choice, the same as for bad non-.php requests (subdirectories, .html, etc.)
#error pages if PHP-FPM is active
RewriteEngine onRewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.+\.php /404.php [L]EDIT: There's another version of this code (2 posts down) that is better to use under most circumstances.
I hope this helps someone! :)
1 -
Thanks for sharing that!
0 -
Thank you PeteS.
Works great
Any idea how to extend that to links without .php at the end ?
For instance www.mydomain.com/blah instead of www.mydomain.com/blah.php
ErrorDocument handles the former just fine as wellAlso, I assume the error doesn't trickle through this way to the client that is making the request ?
If I'm not mistaken the error does go through with ErrorDocument, so that search engines don't index broken links (and don't start indexing 404.php because it's got all those incoming links from broken websites)EDIT. Actually that last bit can be forced by setting the header I believe:
header("HTTP/1.1 404");0 -
Peter, sorry for the late reply...
Only PHP files are handled by PHP-FPM are at issue. Others respond to normal .htaccess ErrorDocument directives, so no need to extend. (But you could modify the RewriteRule to include any invalid requests.)
As for what gets indexed, that may be an issue with it indexing the 404.php file. I currently have been using a version to rewrite all bad PHP file requests (ex: .php .php3 .php4 .php5 .phtml) and redirect them to the ErrorDocument directive, to prevent indexing.
Example:
ErrorDocument 404 /errorpage.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.+\.ph[^.]*$ [L,R=404]0
Please sign in to leave a comment.
Comments
35 comments