Search code examples
.htaccesscustom-error-pages

.htaccess error: cant get custom 400,404,500 error page to appear with html and php redirect


I have code in my .htaccess that is supposed to take www.mysite.com/page.html or www.mysite.com/page.php and just show www.mysite.com/page. This code seems to be interfering with my other code that redirects to custom error pages I have created. I have tested it on another server and found that my code works individually, but not together.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+).* $1.html

<-- this is what i added below and it doesnt work with the code abouve, 
however it does work when the only code in the htaccess file-->

ErrorDocument 400 /400.shtml
ErrorDocument 404 /404.shtml
ErrorDocument 500 /500.shtml

Solution

  • Try it like this:

    ErrorDocument 400 /400.shtml
    ErrorDocument 404 /404.shtml
    ErrorDocument 500 /500.shtml
    
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    ...
    

    The ErrorDocument directives should appear first. It's also noteworthy that you need an absolute path to your error scripts, e.g. /home/web/404.shtml or http://example.com/404.shtml.