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

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

DDD
Release: 2024-11-12 14:17:02
Original
795 people have browsed it

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();
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template