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

拜拜、爱过 提交于 2019-12-13 06:49:35

问题


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!!


回答1:


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'].




回答2:


This syntax is incorrect:

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

Because ErrorDocument has no connection with RewriteCond (mod_rewrite) and it is unconditional.

Conditional 404 handling example:

ErrorDocument 404 /404.php
RewriteRule ^foo/ - [L,NC,R=404]



回答3:


I use this syntax! Usually works!

ErrorDocument 404 "<script>window.location.href='404.php?page='+window.location.href.split('/')[window.location.href.split('/').length-1];</script>"


来源:https://stackoverflow.com/questions/18804740/htaccess-error-404-handling-not-working-dynamic-404-error-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!