How do I force redirect all 404's (or every page, whether invalid or not) to the homepage?

妖精的绣舞 提交于 2019-11-28 04:59:03

Setting the error page to the home page like this

error_page 404 /index.html;

has a small problem, the status code of the home page will be "404 not found", if you want to load the home page with a "200 ok" status code you should do it like this

error_page 404 =200 /index.html;

This will convert the "404 not found" error code to a "200 ok" code, and load the home page

The second method which @jvperrin mentioned is good too,

try_files $uri $uri/ /index.html;

but you need to keep 1 thing in mind, since it's the location / any asset that doesn't match another location and is not found will also load the index.html, for example missing images, css, js files, but in your case I can see you already have another location that's matching the assets' extensions, so you shouldn't face this problem.

To get a true redirect you could do this:

in serverblock define the error-page you want to redirect like this:

# define error page
error_page 404 = @myownredirect;
error_page 500 = @myownredirect;

Then you define that location:

# error page location redirect 302
location @myownredirect {
  return 302 /;
}

In this case errors 404 and 500 generates HTTP 302 (temporary redirect) to / (could of course be any URL)

If you use fast-cgi for php or other these blocks must have the following added to send the errors "upstrem" to server-block:

fastcgi_intercept_errors on;

Try adding the following line after your index definition:

error_page 404 /index.html;

If that doesn't work, try changing your try_files call to the following instead:

try_files $uri $uri/ /index.html;

Hopefully one of those works for you, I haven't tested either yet.

This solution for nginx hosted site:

Edit your virtual hosting file

sudo nano /etc/nginx/sites-available/vishnuku.com 

Add this snippet in the server block

# define error page
error_page 404 = @notfound;

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

In your php block put the fastcgi_intercept_errors set to 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;
}

Final code will look like this

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;
    }
}

Must use

"fastcgi_intercept_errors on;"

along with the custom redirect location like error_page 404 =200 /index.html or

as above

location @myownredirect {
  return 302 /;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!