相关疑难解决方法(0)

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

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

我决定暂时关闭我的网站,并创建了一个简单的单页感谢主页.但旧链接和外部网站仍在尝试访问网站的其他部分,这些部分已不复存在.

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

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

location / {
    try_files $uri $uri/ $document_uri/index.html;
}
Run Code Online (Sandbox Code Playgroud)

我当前的配置是(我现在甚至不提供PHP文件,即主页是简单的html):

server {
    server_name www.example.com example.com;
    access_log /srv/www/example.com/logs/access.log;
    error_log /srv/www/example.com/logs/error.log;
    root /srv/www/example.com/public_html;
    index index.php index.html;

    location / {
        try_files $uri $uri/ $document_uri/index.html;
    }

    # Disable favicon.ico logging
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    # Allow robots and disable logging
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    # Enable permalink structures
    if (!-e $request_filename) {
        rewrite . /index.php last;
    }

    # Handle …
Run Code Online (Sandbox Code Playgroud)

redirect nginx http-status-code-404

30
推荐指数
4
解决办法
6万
查看次数

标签 统计

http-status-code-404 ×1

nginx ×1

redirect ×1