Behseini
Behseini

Reputation: 6320

htaccess 404 is working only on file level but not folder level

I am using this rule to redirect to 404 while there is no such a file on the server

    RewriteEngine on
    ErrorDocument 404 mydomain.com/404
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

this is working fine on for example

mydomain.com/no-exiting-file

but not functioning when I have

mydomain.com/no-exiting-folder/ or mydomain.com/no-exiting-folder/no-exiting-file

How can I fix this to cover not only the none existing file but even the none existing folders as well?

Upvotes: 1

Views: 50

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133518

Could you please try following, written based on your shown samples. Please make sure you clear your browser cache before testing your URLs.

RewriteEngine ON
ErrorDocument 404 mydomain.com/404
RewriteCond %{REQUEST_URI} !\.php/?$ [NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]

Upvotes: 2

Related Questions