2

example.com/home.php <-- file available and Working without .php extension

example.com/home <-- Working

example.com/home1.php <-- File not exist showing 404 error mentioned in htaccess example.com/home1 <-- The file does not exist, and it shows "File not found" . htaccess rule not working here

.htaccess file

RewriteEngine On DirectoryIndex home.php Options -Indexes Options -MultiViews Options +FollowSymLinks RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f # RewriteRule ^([^\.]+)$ $1.html [PT,L] RewriteRule ^([^\.]+)$ $1.php [PT,L] ErrorDocument 404 http://example.com/404.php ErrorDocument 403 http://example.com/403.php 
3
  • I fail to understand what the issue is here. Certainly you will receive a 404 if neither /home1 nor /home1.php exists. That is to be expected and correct. Why do you say " htaccess rule not working here"? To me it looks like the rule does get applied, but /home1.php does not exist. What else should the server respond, if not a 404? Commented Aug 5, 2024 at 16:21
  • @arkascha They are getting the simple response "File not found" (literal text), rather than the expected contents of /404.php. Commented Aug 5, 2024 at 16:35
  • OK, thanks for clearing things up, @MrWhite. So the "rule" said not to work does not actually refer to the RewriteRule, but to the ErrorDocument directive. Commented Aug 6, 2024 at 4:35

1 Answer 1

2

This is probably caused by the way PHP is installed/routed on your system. If you check the HTTP response headers of the "File not found" response, I guess you will see this is from a different/proxy server (perhaps Nginx)?

However, you shouldn't be blindly rewriting requests to .php when the original request simply does not exist. (/home1 is first being internally rewritten to /home1.php, which is then triggering a 404, not /home1 - the original request.) You should instead be checking that <requested-url>.php exists before rewriting the request.

For example, try the following instead:

RewriteCond %{DOCUMENT_ROOT}/$1.php -f RewriteRule ^([^.]+)$ $1.php [L] 

Additional notes...

  • In .htaccess you don't need the PT (pass through) flag, since that is the default action.
  • No need to backslash-escape a literal dot when used inside a regex character class.
  • No need to check that the request does not map to a directory, unless you happen to have a directory of the same name as the .php file basename - but that itself creates a conflict as /<requested-url>.php will not be accessible.
ErrorDocument 404 http://example.com/404.php ErrorDocument 403 http://example.com/403.php 

By specifying an absolute URL in the ErrorDocument directive this will result in a 302 (temporary) redirect to the error document and the original HTTP error status will be lost. (ie. the client sees a 302, not a 404 - bad for users and SEO.)

(And HTTP, not HTTPS?)

This should generally be written using root-relative URL-paths in order to trigger an internal subrequest for the error document (thus preserving information from the original request), not an external redirect. For example:

ErrorDocument 404 /404.php ErrorDocument 403 /403.php 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.