When entering a wrong url, how to direct to the 404 page, can you use JavaScript?

for a project with multiple pages, how to jump to the 404 page written by yourself when you enter the wrong url? is it set on the server side?

is it possible to use JavaScript?

Jul.06,2021

js certainly can't do it. Server processing. He's looking for resources. Found the return found, can not find the return of a default resource, you think 404 . Now it's time for you to give him the 404 you prepared. Just configure it

of course, you just want to judge whether it exists or not. And decided to jump. You can use ajax to request. If you can ask for it. If you do not see the return value of 404, you are doing your operation


Front-end routing can be implemented


and js have nothing to do, because you are not a web service provided by js . Depending on what is installed on your server, nginx , try_files is specifically for this purpose.


You can write this in

vue, but this is a single page

const router = new VueRouter({
    mode: 'history',
    routes: [
        {path:'login', component: Login},
        //
        { path: '*', component: NotFoundPage }
    ]
})
The

404page should be a url, backend visited by your front end that cannot be found, so you will be returned to the configured 404page, and the status code will be changed to 404at the same time.


I think the key is how you define "error" and where the error is judged.


it is recommended that the operation and maintenance staff configure the nginx error page to jump to the specified page! Avoid making two page jumps


this depends on how the project is made up

  1. if it is fully static, it is generally configured on the server side, and even the server side can have a special part to dynamically generate special 404 pages
  2. if it is an ajax dynamic request, the general server only responds to the request and returns a 404 error. Then you need to process this message during ajax processing and navigate to the scheduled relevant page (such as 404 page)
  3. .

configure the page specified by 404 in the server. The ngnix, I use is matched in the configuration file to ok


configure 404 Global Jump specified Page under nginx:

under http module:

http{
    fastcgi_intercept_errors on;
    proxy_intercept_errors on;
    .....
}

under server module:

server {
    error_page 404 = https://your 404 page url;
}

Menu