Home  >  Article  >  Backend Development  >  How to configure 404 page jump in php nginx

How to configure 404 page jump in php nginx

PHPz
PHPzOriginal
2023-04-19 11:36:191461browse

PHP Nginx configuration 404 page jump

Nginx is a high-performance HTTP and reverse proxy server, and PHP is a very popular server-side scripting language. Users usually see a 404 error page when certain files or pages in your website or web application cannot be accessed properly. This is because the server cannot find the requested resource or page, causing a 404 error to be returned. In this case, you can configure Nginx and PHP to redirect to a custom 404 page to improve user experience and provide them with more information.

Configuring Nginx

First, you need to edit the Nginx configuration file. This can be done with the following command:

sudo nano /etc/nginx/sites-available/default

Find the following line in the server block:

location / {

Add the following code to configure Nginx to handle 404 errors:

# if the requested file does not exist, redirect to 404 page
error_page 404 /404.html;
location = /404.html {
    root /var/www/html;
    internal;
}

This will tell Nginx, if the requested page does not exist, it will jump to the 404.html page. If your 404 page is not in the /var/www/html folder, you will need to replace the path with the path to the folder where you actually saved the 404 page.

You can test whether this configuration works using the following code:

sudo nginx -t

If there are no errors, you should see a message indicating that the Nginx configuration file syntax is correct:

nginx: configuration file /etc/nginx/nginx.conf test is successful

Finally, restart the Nginx server for the changes to take effect.

sudo systemctl restart nginx

Configuring PHP

The next step is to configure the PHP interpreter to catch and handle 404 errors. This can be done by adding the following code to the htaccess file in the PHP framework or CMS you are using:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /404.php [L,QSA]

This will tell PHP to jump to 404.php if the requested page does not exist page. If your 404 page is not located in the root of your website, you will need to replace the path with the path to the folder where you actually save the 404 page.

Add this code to the .htaccess file using the following code:

sudo nano /var/www/html/.htaccess

Important: If your website uses Nginx as a front-facing reverse proxy server, you cannot use the .htaccess file. In this case you need to add this code to your PHP code.

Create a 404 page

Finally, you need to create a custom 404 page. This is your opportunity to provide the user with more information about the requested resource or page that cannot be found.

Open your text editor and create a new file called 404.php. You can use the following sample code as a starting point:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>404 Not Found</title>
</head>
<body>
    <h1>404 Not Found</h1>
    <p>The requested URL was not found on this server.</p>
</body>
</html>

You are free to add more details, such as links to other pages or resources you would like users to try, etc.

Place 404 Page

Put your 404.php file into the root directory of your website or application or the specified folder (depending on your PHP code and Nginx configuration ). You can use the following command to copy the file to the correct location:

sudo cp /path/to/404.php /var/www/html/

If you save the 404 page in a different folder, you will need to replace the above path with the folder path.

Complete

After completing all configurations, you should be able to jump to a custom 404 page when a user requests a page or resource not found. Your 404 page should provide more information to users and improve their experience.

The above is the detailed content of How to configure 404 page jump in php nginx. 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