Redirect 如何强制重定向所有404';s(或每个页面,无论是否无效)到主页?

Redirect 如何强制重定向所有404';s(或每个页面,无论是否无效)到主页?,redirect,nginx,http-status-code-404,Redirect,Nginx,Http Status Code 404,目前每个无效页面都是500(内部服务器错误),因为我可能把服务器块配置搞砸了 不久前,我决定关闭我的网站,并创建了一个简单的“谢谢”主页。然而,旧的链接和外部站点仍在尝试访问该站点的其他部分,这些部分已不复存在 如何强制将所有非主页(任何无效URL)重定向到主页 我尝试使用以下块,但不起作用: location / { try_files $uri $uri/ $document_uri/index.html; } 我目前的配置是(我现在甚至不提供PHP文件,ie主页是简单的html)

目前每个无效页面都是500(内部服务器错误),因为我可能把服务器块配置搞砸了

不久前,我决定关闭我的网站,并创建了一个简单的“谢谢”主页。然而,旧的链接和外部站点仍在尝试访问该站点的其他部分,这些部分已不复存在

如何强制将所有非主页(任何无效URL)重定向到主页

我尝试使用以下块,但不起作用:

location / {
    try_files $uri $uri/ $document_uri/index.html;
}
我目前的配置是(我现在甚至不提供PHP文件,ie主页是简单的html):


尝试在
索引
定义后添加以下行:

error_page 404 /index.html;
如果不起作用,请尝试将您的
try\u文件
调用改为以下内容:

try_files $uri $uri/ /index.html;

希望其中一个对您有效,我也还没有测试过。

将错误页面设置为如下主页

error_page 404 /index.html;
error_page 404 =200 /index.html;
server {
    listen 80;
    server_name vishnuku.com;

    root /var/www/nginx/vishnuku.com;
    index index.php index.html;

    access_log /var/log/nginx/vishnuku.com.log;
    error_log /var/log/nginx/vishnuku.com.error.log;

    location / {
        try_files $uri $uri/ /index.php?$args /;
    }

    # define error page
    error_page 404 = @notfound;

    # error page location redirect 301
    location @notfound {
        return 301 /;
    }

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_intercept_errors on;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~ /\.ht {
        deny all;
    }

    location = /nginx.conf {
        deny all;
    }
}
有一个小问题,主页的状态代码将是“404未找到”,如果你想加载带有“200 ok”状态代码的主页,你应该这样做

error_page 404 /index.html;
error_page 404 =200 /index.html;
server {
    listen 80;
    server_name vishnuku.com;

    root /var/www/nginx/vishnuku.com;
    index index.php index.html;

    access_log /var/log/nginx/vishnuku.com.log;
    error_log /var/log/nginx/vishnuku.com.error.log;

    location / {
        try_files $uri $uri/ /index.php?$args /;
    }

    # define error page
    error_page 404 = @notfound;

    # error page location redirect 301
    location @notfound {
        return 301 /;
    }

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_intercept_errors on;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~ /\.ht {
        deny all;
    }

    location = /nginx.conf {
        deny all;
    }
}
这将把“404NotFound”错误代码转换为“200OK”代码,并加载主页

@jvperrin提到的第二种方法也很好

try_files $uri $uri/ /index.html;

但是您需要记住一件事,因为它是
位置/
任何与其他位置不匹配且未找到的资产也将加载
index.html
,例如缺少图像、css、js文件,但在您的情况下,我可以看到您已经有另一个与资产扩展名匹配的位置,所以你不应该面对这个问题。

要获得真正的重定向,你可以这样做:

在服务器块中,按如下方式定义要重定向的错误页:

# define error page
error_page 404 = @myownredirect;
error_page 500 = @myownredirect;
然后定义该位置:

# error page location redirect 302
location @myownredirect {
  return 302 /;
}
在这种情况下,错误404和500生成HTTP 302(临时重定向)到/(当然可以是任何URL)

如果您使用fast cgi for php或其他程序块,则必须添加以下程序块才能将错误“upstrem”发送到服务器程序块:

fastcgi_intercept_errors on;
必须使用

"fastcgi_intercept_errors on;"
以及自定义重定向位置,如
错误\u第404页=200/index.html

如上

location @myownredirect {
  return 302 /;
}

针对nginx托管站点的此解决方案:

编辑虚拟主机文件

sudo nano /etc/nginx/sites-available/vishnuku.com 
在服务器块中添加此代码段

# define error page
error_page 404 = @notfound;

# error page location redirect 301
location @notfound {
    return 301 /;
}
在php块中,将fastcgi_intercept_errors设置为on

location ~ \.php$ {
    include /etc/nginx/fastcgi_params;
    # intercept errors for 404 redirect
    fastcgi_intercept_errors on;
    fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
最终代码如下所示

error_page 404 /index.html;
error_page 404 =200 /index.html;
server {
    listen 80;
    server_name vishnuku.com;

    root /var/www/nginx/vishnuku.com;
    index index.php index.html;

    access_log /var/log/nginx/vishnuku.com.log;
    error_log /var/log/nginx/vishnuku.com.error.log;

    location / {
        try_files $uri $uri/ /index.php?$args /;
    }

    # define error page
    error_page 404 = @notfound;

    # error page location redirect 301
    location @notfound {
        return 301 /;
    }

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_intercept_errors on;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~ /\.ht {
        deny all;
    }

    location = /nginx.conf {
        deny all;
    }
}

首先,有很多不同的方法将所有404重定向到主页 这有助于你在搜索引擎优化 利用

 fastcgi_intercept_errors on;
然后将以下内容添加到您的配置中

        error_page 404 =301 http://yourdomain.com/;
    error_page 403 /error403.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
            root /var/www/html;

    }

我来这里是为了实现类似的功能,我使用Nginx作为自托管的反向代理,上面的答案都没有。因此,如果您正在使用
proxy\u pass
并截获错误,您还可以处理此逻辑以重定向到
index.html
页面

处理URL,例如:

  • http://localhost/ -200行
  • http://localhost/index.html -200行
  • http://localhost/non-existant/ -404找不到
服务器{
听80;
服务器名称localhost;
地点/{
重写^/$/static/index.html break;
上的代理截获错误;
代理通行证http://something-to-proxy/static/;
}
错误\u第404页/index.html;
}

我想这会管用的,除了我对任何无效页面都会得到500而不是404。你能看到我的配置还有什么问题吗?当我进入,比如说,
www.example.com/asdasasd/
(它不存在)时,我在内部重定向到“404”时遇到了一个类似于
重写或内部重定向循环的错误。
错误页面
尝试文件
都不能一起工作,因此,请分别尝试它们,而不是同时尝试两者,因为我认为它们一起使用时会重定向两次。这与
location~\.php$
块有关,因为如果我注释掉整个块,它可以与
error\u页面
try\u文件一起使用。不幸的是,我真的不知道为什么php块会破坏这个配置。我想不出有什么情况需要“覆盖”真正的http状态代码。如果找不到一个页面,它应该返回404状态码。我记得我们曾经在工作中需要这样做,但我想不起为什么。@AlanChavez如果你正在经营一家商店,所有访问你网站的访问者都应该是“可转换的”。如果有人得到一个旧的或损坏的URL,并看到404页面,他价值0美元。如果你将他重定向到主页,他可能会四处看看。一直发送200状态码也可以确保跟踪和分析系统获得尽可能多的数据——许多系统会看到404状态,并假设页面没有任何结果。@William your提出了一个很好的观点,尽管据我所知,最大的搜索引擎不赞成“软404”(对于404页,返回非404 | 410状态代码的页)因为否则,机器人会抓取和索引一个不存在的页面。我从来没有遇到过分析不跟踪返回404代码的页面的问题,但当然我还没有尝试过所有的解决方案。@ILikeTacos实际上这是一个非常普遍的需要。单页web应用需要将路由重定向回单页。这对我很有用。你救了我一天,投票吧。