C. Ovidiu
C. Ovidiu

Reputation: 1134

apache/.htaccess: incorrect page does not redirect to custom 404

I have a strange error where my ErrorDocument 404 in .htaccess is not working properly in all cases.

If I visit a non-existing page I simply get the message File not found..

If tho, I add the .html extension, it shows the custom 404 page I created.

https://website.com/not-found

This link does not work and I simply get the message File not found.

https://website.com/not-found.html

Visiting this link however, I see my custom 404 page.

Here is my .htaccess file

<IfModule mod_rewrite.c>

    Options +FollowSymLinks -MultiViews
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+?)/?$ $1.php [NC,L]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
    RewriteRule ^(.+)\.php$ /$1 [R=301,L]

    ErrorDocument 404 /app/templates/errors/404.php
    ErrorDocument 403 /app/templates/errors/403.php

    # Block access to hidden files and directories.
    RewriteCond %{SCRIPT_FILENAME} -d [OR]
    RewriteCond %{SCRIPT_FILENAME} -f
    RewriteRule "(^|/)\." - [F]

</IfModule>

Basically I strip the extension and that is all. On other websites this code works good, could it be the hosting company the problem ? Note that I do not have access to the apache config file.

Thank you

Upvotes: 1

Views: 212

Answers (1)

anubhava
anubhava

Reputation: 785276

Change your first like this to check for existence of .php file:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]

In absence of RewriteCond %{REQUEST_FILENAME}.php -f it rewrites every non-file and non-directory.

Upvotes: 1

Related Questions