简体   繁体   English

Jetty 配置将所有 404 重定向到主页

[英]Jetty config to redirect all 404s to home page

Google has cached some old URLs to pages that no longer exist on a site. Google 已将一些旧 URL 缓存到站点上不再存在的页面。 I'd like to redirect any 404 pages to the home page.我想将任何 404 页面重定向到主页。

I have a jetty installation with a ROOT.war file installed in jetty/webapps.我在jetty/webapps 中安装了一个带有ROOT.war 文件的jetty 安装。 The ROOT.war file contains a WEB-INF/web.xml file that has the following in it: ROOT.war 文件包含一个 WEB-INF/web.xml 文件,其中包含以下内容:

<error-page>
    <error-code>404</error-code>
    <location>/</location>
</error-page>

However, this is only redirecting top level files, nothing in subdirectories.但是,这只是重定向顶级文件,在子目录中没有任何内容。 So the following URL get redirected to the home page:因此,以下 URL 被重定向到主页:

http://mysite.com/pageDoesntExist.html

But this one doesn't and just gives a 404 error:但是这个没有,只是给出了一个 404 错误:

http://mysite.com/directoryDoesntExist/pageDoesntExist.html

Is there a way to configure all 404's to go to the home page?有没有办法将所有 404 配置为转到主页? Can I do this in the jetty/contexts directory somehow?我可以在 jetty/contexts 目录中以某种方式执行此操作吗?

You want to use the org.mortbay.jetty.handler.MovedContextHandler: http://jetty.mortbay.org/xref/org/mortbay/jetty/handler/MovedContextHandler.html你想使用 org.mortbay.jetty.handler.MovedContextHandler: http : //jetty.mortbay.org/xref/org/mortbay/jetty/handler/MovedContextHandler.html

Bots will use the Header 301 Redirect to update the cache google Bots 将使用 Header 301 Redirect 来更新缓存 google

An idea is:一个想法是:

<error-page>
    <error-code>404</error-code>
    <location>/error404.html</location>
</error-page>

(as described in Redirecting a 404 error page to a custom page of my Spring MVC webapp in Tomcat ) and then inside error404.html do a redirect: (如Redirecting a 404 error page to a custom page of my Spring MVC webapp in Tomcat 中所述)然后在error404.html进行重定向:

<html>
<head><title>Redirecting...</title></head>
<body>
    <script>
    document.location.href="/";
    </script>
</body>
</html>

(as described in how to redirect to home page ) (如如何重定向到主页中所述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM