【问题标题】:Display Homepage on 404 Error在 404 错误上显示主页
【发布时间】:2018-08-11 02:46:13
【问题描述】:

我正在尝试将 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】:

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

    error_page 404 = /index.php;
    

    详情请见this document


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

    try_files $uri /index.php =404;
    

    请参阅this document 了解更多信息。

    【讨论】:

    • 感谢您的帮助。我尝试了您的两种解决方案,但不幸的是都没有解决我的问题。 index.php 在我访问此网址 https://www.website.com/directory/page 时显示。但是,当我仅使用 HTTP (http://www.website.com/directory/page) 访问相同的 URL 时,它不会显示 index.php,它只会显示一个空白页。关于如何使它也适用于 HTTP 请求的任何建议?谢谢。
    猜你喜欢
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 2017-03-12
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 2012-11-22
    • 2012-07-12
    相关资源
    最近更新 更多