简体   繁体   English

重定向到我的custom404错误页面php

[英]redirect to my custom404 error page php

I have a custom 404 error page in my root. 我的根目录中有一个自定义404错误页面。

I have edited my .httaccess file also. 我也编辑了.httaccess文件。

Now I want to redirect to my custom 404 page,if certain condition fails in my page. 现在,如果某些情况在我的页面中失败,我想重定向到我的自定义404页面。

For example $_SESSION[log]==1 fails. 例如,$ _SESSION [log] == 1失败。 So my question is 所以我的问题是

1.What code should I use in php to do so? 1.我应该在php中使用什么代码呢?

2.how to I make other types of error,like 401 and many other types of errors? 2.如何制作其他类型的错误,例如401和许多其他类型的错误?

In my .httaccess page,I have these lines 在我的.httaccess页面中,这些行

ErrorDocument 404 http://mydomain.com/404custom.php

And I have a 404custom.php designed to my needs..Simple HTML page... 我有一个404custom.php可以满足我的需求。简单的HTML页面...

Don't send a Location header in error cases. 错误情况下请勿发送Location标头。 Send the correct status code instead. 而是发送正确的状态代码。 You must send the correct status code according to the http specification. 您必须根据http规范发送正确的状态代码。

header("HTTP/1.0 404 Not Found");

For showing the correct custom site, you have already your ErrorDocument statement in the .htaccess file. 为了显示正确的自定义站点,您已经在.htaccess文件中包含ErrorDocument语句。

Especially for the 403 unauthorized error sending the correct status code is important, so the browser can react accurate. 特别是对于403未经授权的错误,发送正确的状态代码非常重要,因此浏览器可以做出准确的反应。

1- To redirect your user you can use : 1-要重定向您的用户,您可以使用:

<?php
   header('Location: http://www.example.com/404.html');
?>

But you must not send any data before using header() no echo and not even blank space. 但是,在使用header()之前,不要发送任何数据,不要echo ,甚至不要使用空格。

2- For other errors : 2-对于其他错误:

ErrorDocument 400 /var/www/errors/index.html
ErrorDocument 401 /var/www/errors/index.html
ErrorDocument 403 /var/www/errors/index.html
ErrorDocument 404 /var/www/errors/index.html
ErrorDocument 500 /var/www/errors/index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM