Forum Moderators: phranque

Message Too Old, No Replies

404 page not showing for PHP pages

gives "file not found" text / works fine for html pages

         

lee_sufc

10:22 am on May 7, 2024 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hi all. Long time no post!

I have an odd issue, which I can only assume is htaccess-related.

Basically, if you try to access a non-existent HTML page on my site, you'll see a 404 error page as expected.

However, for PHP pages, you'll see a blank page with the text, "File not found" (although the headers do return a 404 response when I've used online tools to check).

Here's my htaccess:

#Gzip



AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript


#End Gzip

#cache html and htm files for one day

<FilesMatch "\.html?$">

Header set Cache-Control "max-age=43200"

</FilesMatch>

#cache css, javascript and text files for one week

<FilesMatch "\.(js|css|txt)$">

Header set Cache-Control "max-age=604800"

</FilesMatch>

#cache flash and images for one month

<FilesMatch "\.(flv|swf|ico|gif|jpe?g|png)$">

Header set Cache-Control "max-age=2592000"

</FilesMatch>

#disable cache for script files

<FilesMatch "\.(pl|php|cgi|spl|scgi|fcgi)$">

Header unset Cache-Control

</FilesMatch>

RewriteEngine on



RewriteCond %{THE_REQUEST} \.php
RewriteRule ^blog/([^.]+)\.php$ https://www.example.com/blog/$1/ [R=301,L]

RewriteRule ^blog/([^.]+[^./])$ https://www.example.com/blog/$1/ [R=301,L]

RewriteCond %{REQUEST_URI} ^/((?:[\w-]/)*)index\.htm
RewriteRule index\.htm https://www.example.com/%1 [R=301,L,NS]

RewriteCond %{REQUEST_URI} ^/([\w-]+/)blog/index\.php
RewriteRule /blog/index\.php$ https://www.example.com/%1 [R=301,L,NS]

#Redirect old affs



RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]


# rewrite after redirects
RewriteRule ^(blog/[^.]+)/$ /$1.php [L]

# BEGIN cPanel-generated php ini directives, do not edit
# Manual editing of this file may result in unexpected behavior.
# To make changes to this file, use the cPanel MultiPHP INI Editor (Home >> Software >> MultiPHP INI Editor)
# For more information, read our documentation (https://go.cpanel.net/EA4ModifyINI)
<IfModule php7_module>
php_flag display_errors Off
php_value max_execution_time 30
php_value max_input_time 60
php_value max_input_vars 1000
php_value memory_limit 128M
php_value post_max_size 16M
php_value session.gc_maxlifetime 1440
php_value session.save_path "/var/cpanel/php/sessions/ea-php70"
php_value upload_max_filesize 2M
php_flag zlib.output_compression Off
</IfModule>
<IfModule lsapi_module>
php_flag display_errors Off
php_value max_execution_time 30
php_value max_input_time 60
php_value max_input_vars 1000
php_value memory_limit 128M
php_value post_max_size 16M
php_value session.gc_maxlifetime 1440
php_value session.save_path "/var/cpanel/php/sessions/ea-php70"
php_value upload_max_filesize 2M
php_flag zlib.output_compression Off
</IfModule>
# END cPanel-generated php ini directives, do not edit

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php70” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php70 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

not2easy

1:50 pm on May 7, 2024 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I do not see an Error Document statement listed, there should be a line like
ErrorDocument 404 /404.html
or whatever file you want to use to respond to the 404 error in there somewhere to tell the server what URL to serve for a 404 error.
The error page/document should be informative and offer some alternate action other than closing the page. Maybe links to categories or departments that exist to help them find what they expected.

lucy24

3:56 pm on May 7, 2024 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The 404 page is sent out by the server when there is a server-generated 404 response as shown in server access logs. If a page is generated by php, as seems to be the case here, the server itself will send a 200 response because it has successfully found index.php or whatever your php-building page is physically called. If the content-to-be-included doesn’t exist, you have to do two things in your index.php: explicitly send back a 404 response--it sounds like this part is happening--and explicitly include the full content of your custom 404 page.

:: shuffling papers, because I haven't used this since 2014 or so ::
if ($done == 0)
{
ob_end_clean();
if ($realpage == 0)
{
if (function_exists('http_response_code'))
{ http_response_code(404); }
else
{ header($_SERVER['SERVER_PROTOCOL'] . " 404 Not Found"); }
include ($_SERVER['DOCUMENT_ROOT'] . "/boilerplate/missing.html");
}
}
Edit out the parts that don’t apply; I think you can figure out what my variable-names meant. My version involved an output buffer, hence the ob_end_clean. But there should definitely be something analogous to the last two lines.

[edited by: engine at 6:58 am (utc) on May 8, 2024]
[edit reason] Edit requested by poster [/edit]