开发者

Problems with 404 redirect with apache and php

I have a site which, for a long time, redirect the user directly to the hompage if the url was incorrect. Instead of returning 404 error, the script used header('location: /'); die(); .

I've changed that line to header("HTTP/1.0 404 Not Found");die(); and the server started to send the 404 error message.

But, now I get the browser 404 error and not my custom 404 page. So, I've opened .htaccess file and added

开发者_如何学JAVAErrorDocument 404 /index.php

But no success, I'm still getting the browser 404 page and not my 404 page (I've also tried to set it under /etc/apache2/sites-available/site-conf)

Also tried:

    $_SERVER['REDIRECT_STATUS'] = 404;
    header("HTTP/1.1 404 Not Found");
header('location: /');
    die();

And still - no success

Any Ideas?


header("HTTP/1.1 404 Not Found");
include('index.php'); // maybe you have to adjust the path;
exit;

When you use header(location) you are setting the status to 3xx. And if the status is 404, browser does not need to follow any location headers. It should follow the location header only in 3xx responses.


you are mixing some things.

when your script runs, this means that there is a page. within your script you can control the status via header("HTTP/1.1 404 Not Found"); this say the browser "not found". finally a page is served with a 404 status. all output of that page is the errorpage. everything is done by your script.

when you are define within a htaccess file a 404 error page, then this page is display if the apache server handles the not found error. the means your browser has to access a file which is not present on the server. then the file index.php is displayed.

header status and header location cannot used together. a location command sets the status to a redirect code 3xx. after redirection the server serves the page with code 200.

what you can do is, redirect the client to a random url on your server. then the server is displaying the 404 page.


First check if .htaccess files are enabled in your Apache configuration (AllowOverride directive). The ErrorDocument you have tried should work!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜