显示主页404错误

时间:2018-03-02 20:17:02

标签: php .htaccess nginx server

我正在尝试将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;
    }

}

1 个答案:

答案 0 :(得分:0)

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

try_files

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

或者,如果您的第二个/index.php语句生成了有问题的404响应,请在其中添加try_files $uri /index.php =404; 。例如:

df <- read.table(text = "Name1    Col1    Col2    Col3    Name2    Col4    Col5    Col6    Col7
John     A       A       A       Alex       B       B       B      1
                  Alex     B       B       B       John       A       A       A      0'", 
                 header = TRUE, stringsAsFactors = FALSE)


for(i in 1:nrow(df)){
  if(df$Col7[i] == 1){
    df1 <- df[i, c("Name1", "Col1", "Col2", "Col3")]
  }else if(df$Col7[i] == 0){
    df2 <- df[i, c("Name2", "Col4", "Col5", "Col6")]
  }
}

colnames(df2)[1] <- "Name1"
colnames(df2)[2] <- "Col1"
colnames(df2)[3] <- "Col2"
colnames(df2)[4] <- "Col3"

df <- rbind(df1, df2)

   Name1 Col1 Col2 Col3
1  John    A    A    A
2  John    A    A    A

有关详情,请参阅this document

相关问题