SOLVED "File not found" appears instead of 404.shtml (easy htaccess solution)

Mar 17, 2016
18
1
3
cPanel Access Level
Root Administrator
"File not found" simple error message text appears instead of a custom 404.shtml file whenever a .php extension such as /not-a-file.php targeted. Non .php files work as expected on error such as /not-a-file.html for example.

I spent a couple of hours messing around with Apache and htaccess settings trying to sort this out. I just migrated to a new server, always fun :( The forums here show several posts that didn't appear to help such as ProxyErrorOverride On.

What is wrong with this simple .htaccess fix?

# High up in the .htaccess file.
ErrorDocument 404 /404.shtml

# After your last rewrite rule.
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^.+\.php$ /404 [NC,L]

The above works for me without any Apache changes. Beware there is an similar rule out that that throws a 200 status instead of a 404 very important. Ensure you get a 404 upon inspection in F12 Google Chrome for example.

Any insight would be appreciated.
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,909
2,228
463
Hello John,

I removed the previous posts to this thread as there appears to have been some confusion about the question you were asking.

What is wrong with this simple .htaccess fix?

# High up in the .htaccess file.
ErrorDocument 404 /404.shtml

# After your last rewrite rule.
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^.+\.php$ /404 [NC,L]

The above works for me without any Apache changes.
"Why is ProxyErrorOverride On better than my htaccess method?"
You can setup the rules via your .htaccess file or via a custom Apache include. Using a .htaccess file is actually less likely to result in issues with Service Subdomains (e.g. webmail.domain.tld) per the discussion on the thread below:

"File not found." instead of custom 404 file with php-fpm enabled

Thus, I don't believe using a custom Apache include is better than the method you provided.

Let me know if that helps to clarify.

Thank you.
 
  • Like
Reactions: John Napoletano
Mar 17, 2016
18
1
3
cPanel Access Level
Root Administrator
Hello John,

I removed the previous posts to this thread as there appears to have been some confusion about the question you were asking.

You can setup the rules via your .htaccess file or via a custom Apache include. Using a .htaccess file is actually less likely to result in issues with Service Subdomains (e.g. webmail.domain.tld) per the discussion on the thread below:

"File not found." instead of custom 404 file with php-fpm enabled

Thus, I don't believe using a custom Apache include is better than the method you provided.

Let me know if that helps to clarify.

Thank you.
@cPanelMichael Thank you for clarifying.

There was also a piece of the puzzle relating to log errors such as this one:

mod_include: Options +Includes (or IncludesNoExec) wasn't set, INCLUDES filter removed: /404.shtml

There are settings for these options, checkboxes, accessible from WHM. Ultimately my problem in regards to the log errors, I believe was that I first disabled all 'Options' using htaccess, then forgot to add back the option as indicated by the log error. Here is the snippet of the htaccess related to the mod_include error if that helps anyone. If it's confusing then post here and I'll try to clarify. It's not fresh in my mind at the moment.

# Options
Options None
Options +SymLinksIfOwnerMatch +IncludesNOEXEC -Indexes


The mod_include log error stopped appearing after making the above change with the +IncludesNOEXEC being the piece that I was missing per the error message. Putting it all together would include the following lines in my htaccess, other lines left out to simplify:

# High up in the .htaccess file.
Options None
Options +SymLinksIfOwnerMatch +IncludesNOEXEC -Indexes
ErrorDocument 404 /404.shtml

# After your last rewrite rule.
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^.+\.php$ /404 [NC,L]
 

Arty2

Registered
Mar 6, 2020
1
0
1
Zimbabwe
cPanel Access Level
Website Owner
"File not found" simple error message text appears instead of a custom 404.shtml file whenever a .php extension such as /not-a-file.php targeted. Non .php files work as expected on error such as /not-a-file.html for example.

I spent a couple of hours messing around with Apache and htaccess settings trying to sort this out. I just migrated to a new server, always fun :( The forums here show several posts that didn't appear to help such as ProxyErrorOverride On.

What is wrong with this simple .htaccess fix?

# High up in the .htaccess file.
ErrorDocument 404 /404.shtml

# After your last rewrite rule.
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^.+\.php$ /404 [NC,L]

The above works for me without any Apache changes. Beware there is an similar rule out that that throws a 200 status instead of a 404 very important. Ensure you get a 404 upon inspection in F12 Google Chrome for example.

Any insight would be appreciated.
Could you just share fix I don't have coding experience
 

Rajeeva Lochana

Well-Known Member
Community Guide Contributor
May 27, 2019
122
38
28
India
cPanel Access Level
Root Administrator
Also, this is not related, but, ALL OF THE ERROR PAGES ARE CONTROLLED BY APACHE'S SOURCE CODE, SO YOU WILL HAVE TO RECOMPILE APACHE AFTER EDITING THE SOURCE. I DON'T THINK ANYONE IS ALLOWED TO DO THAT, ALSO THAT WOULD BE HARD, YOU CAN CUSTOMIZE THE ERROR PAGES, BUT NOT THE ERROR PAGES LIKE 414 REQUEST URI TOO LONG, SO YOU CANNOT CUSTOMIZE EVERY ERROR DOCUMENT