在404错误上显示主页

问题描述 投票:1回答:1

我正在尝试将404请求重定向到我们网站的主页,这只是index.php。收到404错误消息的示例URL是此URL https://www.website.com/directory/page。我尝试将以下行添加到我的配置文件中:error_page 404 /index.php;。当我这样做时,我不再收到HTTPS请求的404。它显示我希望的主页。但我仍然在HTTP请求上收到404错误。关于如何让它在HTTP上工作的任何建议?

我目前正在运行NGINX服务器。这是我配置文件的一部分:

server {
    server_name acme.com;
    return 301 $scheme://www.acme.com$request_uri;
}

server {
    listen 80;
    server_name www.acme.com;

    #BEGIN SSL
    listen 443 ssl;
    ssl_certificate /etc/nginx/ssl/acme.com.chained.crt;
    ssl_certificate_key /etc/nginx/ssl/acme.com.key;

    # Makes for the most secure connections
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers AES256+EECDH:AES256+EDH:!aNULL;
    #END SSL

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ $uri.html $uri.php?$query_string;
    }

    error_page 404 /index.php;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

}
php .htaccess nginx server
1个回答
0
投票

您可以在error_page语句中更改响应代码。要返回PHP脚本生成的响应代码,请使用:

error_page 404 = /index.php;

有关详细信息,请参阅this document


或者,如果有问题的404响应是由您的第二个try_files语句生成的,请在那里添加/index.php。例如:

try_files $uri /index.php =404;

有关更多信息,请参阅this document

© www.soinside.com 2019 - 2024. All rights reserved.