Home  >  Article  >  Backend Development  >  How to Simulate Error 404 Pages in PHP Using .htaccess?

How to Simulate Error 404 Pages in PHP Using .htaccess?

DDD
DDDOriginal
2024-11-12 14:17:02728browse

How to Simulate Error 404 Pages in PHP Using .htaccess?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn