Simulating Error 404 Pages in PHP
Question: When handling requests through a .htaccess file, how can you simulate error 404 pages using PHP if the requested page is not found?
Answer:
The header function does not create a true 404 page. Instead, you can use the http_response_code function to generate a 404 status code, as shown below:
<?php http_response_code(404); include('my_404.php'); // Provide your own HTML for the error page die();
The die() function ensures that the normal execution is terminated after displaying the error page. This method effectively triggers the display of your custom 404 page configured via ErrorDocument in .htaccess.
The above is the detailed content of How to Simulate Error 404 Pages in PHP Using .htaccess?. For more information, please follow other related articles on the PHP Chinese website!