Davis LF Davis LF - 7 months ago 28
PHP Question

Return 404 page when someone access any .php?

I always hide the files .php extension in .htaccess. However, if anyone access the .php file returns a unexpected error page that should be 404 page in the current .htaccess text.

My actual text (from @anubhava answer):

# to present a custom 404 page
ErrorDocument 404 ./error

RewriteEngine On

# return 404 for direct .php requests
RewriteCond %{THE_REQUEST} \.php[/\s?] [NC]
RewriteRule ^ - [R=404,L]

# silent rewrite to add .php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]


Just a test. Edit: I believe the error is on the 404 document, as the 404 error is being returned now. Instead of appear the 404 page, it appears the directory I input in my browser page, as below:


./error

Answer

Here is a rule you place just below RewriteEngine On to present 404 for the requests for .php files:

# to present a custom 404 page
ErrorDocument 404 /errorPage404.php

RewriteEngine On

# return 404 for direct .php requests
RewriteCond %{THE_REQUEST} \.php[/\s?] [NC]
RewriteRule ^ - [R=404,L]

# silent rewrite to add .php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Comments