问题
I have a custom 404 error page on my site, but while doing other things on the site, the error page makes it hard to figure out what my missing file problems are because it redirects to a given html or php file and takes the URL of the missing file out of the address bar and replaces it with the URL of the custom error page.
So for example, when users log in to the site, I have it link to a php file when they hit submit, but then the browsers takes them straight to the 404 error page, so I can't see that the problem is actually a link to one directory higher than it should be.
I've tried printing the URL of the missing file to the page using php, but the server just grabs the URL of the 404 error page, rather than the original missing file URL. Here is how I tried doing that: http://members.cox.net/midian/tutorials/php404.htm
How can I either show the custom error page while keeping the original missing URL in the address bar, or print out the URL of the missing file to the page?
回答1:
If your error pages are redirected to a PHP powered 404 page then the variable $_SERVER["HTTP_REFERER"]
should contain something interesting. Having said that, redirecting to a error-404 page is a bad practice. It confuses search engines and human visitors alike.
Edit
Here is a solution that works on Apache. Add this line to your .htaccess
file:
ErrorDocument 404 /error-404.php
Create a file called error-404.php
in the root directory of your website with the following content:
<?php
echo "<h1>404 Not Found</h1>";
echo "<p>The page {$_SERVER['REQUEST_URI']} was not found on this server</p>";
回答2:
What Salman A posted works for me.
It only worked though when I used a relative path in the .htaccess file:
ErrorDocument 404 /error-404.php
and not ErrorDocument 404 http://www.mydomain.com/error-404.php
The second version was giving me the redirected url in the address bar and in the page body.
Also, you don't need to use a php page for your 404 message; an html page works as well if you don't want to echo the URL in the body of the page.
来源:https://stackoverflow.com/questions/7426793/404-custom-error-page-redirect-doesnt-show-missing-file-url