“Spoofing” a 404 not found error with .htaccess

前端 未结 1 1790
遥遥无期
遥遥无期 2021-01-21 05:14

I have .htaccess currently set up to rewrite anything without a period or slash to the equivalent with a .php extension (so \"foo\" internally pulls up \"foo.php\") via the foll

相关标签:
1条回答
  • 2021-01-21 05:47

    Found a solution:

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ (/.*)\.php
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteCond %{REQUEST_URI} !^/error\.php
    RewriteRule . - [R=404,L,NS]
    
    ErrorDocument 404 /error.php?e=404
    

    Specifically, the third RewriteCond keeps requests for the error page from triggering the RewriteRule (since the THE_REQUEST still contains the original request (matching the first RewriteCond), and the error page does indeed exist (matching the second RewriteCond).

    Note that the third RewriteCond matches the URI format specified for the ErrorDocument. Within error.php, I use $_SERVER["REDIRECT_SCRIPT_URL"] to find out what file the user originally requested (thus making it look like the 404 was served as a direct response to their request).

    0 讨论(0)
提交回复
热议问题