Method for ThinkPHP to access non existent modules and jump to 404 pages

  • 2021-07-02 23:37:06
  • OfStack

First, create a new file EmptyAction. class. php in ACTION with the following code:


<?php 
 class EmptyAction extends Action{ 
 function _empty(){ 
  header("HTTP/1.0 404 Not Found");// Cause HTTP Return 404 Status code  
  $this->display("Public:404"); 
 } 
 } 
 ?>

In the case of apache server, you need to add ErrorDocument 404/404. html to the website configuration in apache.

In the case of an iis server, the 404 error page is set under IIS/ASP. net in iis.

Open the apache httpd. conf configuration file or create a new. htaccess configuration file

First, modify the settings of the application root directory, open the "web. config" file edit, and add the following:


<configuration>
 <system.web>
 <customErrors mode= " On "  defaultRedirect= " error.asp " >
 <error statusCode= " 404 "  redirect= " notfound.asp "  />
 </customErrors>
 </system.web>
 </configuration>

Note: In the above example, "error. asp" is the default 404 page of the system, and "notfound. asp" is the custom 404 page. Please modify the corresponding file name when using it.
Then, in the custom 404 page "notfound. asp", add:


<%
 Response.Status =  " 404 Not Found " 
 %>
 

Page 404 of php:


 if(// If there is no result )
   {
   // It used to just show the prompt "This post no longer exists", but now it is: 
   require('/404.php');
   @header('HTTP/1.1 404 Not Found'');
   @header('Status: 404 Not Found');
   exit;
   }
 

Related articles: