Search code examples
.htaccesserrordocument

htaccess error 404 handling not working + dynamic 404 error page?


First of all, my ErrorDocument 404 /v1 is never redirecting, but if I look in the Chrome Inspector, I really have a 404 error. At first I thought it was because I really needed to specify a file ex: error.html, but that didn't work either. /v1 is just a wordpress subdomain, and I want to redirect all of my errors there. /v1 falls on my homepage of my subdomain.

Also I would like to make the error page dynamic, but I can't figure how to do that. Something like that:

RewriteCond %{REQUEST_FILENAME} public_html/(.*)
ErrorDocument 404 /v1/%1

Any ideas? Thank you!!


Solution

  • You're goinmg to need to return the 404 through a php script:

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

    And in the 404.php file:

    <?php
    header("HTTP/1.0 404 Not Found");
    
    print('The file that you requested: ' . $_GET['path'] . ' was not found\n');
    
    // or include a special 404 page
    
    include ('/path/to/' . $_GET['path']);
    ?>
    

    Or you could lose the ?path=$1 part completely and just look in $_SERVER['REQUEST_URI'].