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

后端 未结 6 1428
孤街浪徒
孤街浪徒 2020-12-07 19:06

Currently every invalid page is 500 (Internal Server Error) because I probably messed up with my server block configuration.

I decided to shut down my website a whil

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-07 19:44

    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.

提交回复
热议问题