【问题标题】:Jetty config to redirect all 404s to home pageJetty 配置将所有 404 重定向到主页
【发布时间】:2011-07-22 15:02:58
【问题描述】:

Google 已将一些旧网址缓存到网站上不再存在的网页。我想将任何 404 页面重定向到主页。

我在 jetty/webapps 中安装了一个带有 ROOT.war 文件的 jetty。 ROOT.war 文件包含一个 WEB-INF/web.xml 文件,其中包含以下内容:

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

但是,这只是重定向顶级文件,子目录中没有任何内容。所以下面的 URL 会被重定向到主页:

http://mysite.com/pageDoesntExist.html

但是这个没有,只是给出了 404 错误:

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

有没有办法将所有 404 配置为转到主页?我可以以某种方式在 jetty/contexts 目录中执行此操作吗?

【问题讨论】:

    标签: java jetty


    【解决方案1】:

    你想使用 org.mortbay.jetty.handler.MovedContextHandler: http://jetty.mortbay.org/xref/org/mortbay/jetty/handler/MovedContextHandler.html

    机器人将使用 Header 301 Redirect 来更新缓存 google

    【讨论】:

    • 谢谢。但我不清楚如何配置它来处理大量的 URL。我必须为每个 URL 创建一个新的上下文文件,这太疯狂了。一定会有更好的办法。能否举个例子,比如将/directoryDoesntExist目录下的所有地址都重定向到首页?
    【解决方案2】:

    一个想法是:

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

    (如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>
    

    (如how to redirect to home page中所述)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-13
      • 1970-01-01
      • 2017-08-07
      • 1970-01-01
      • 2020-09-30
      • 2018-03-28
      • 2013-12-04
      • 2013-08-17
      相关资源
      最近更新 更多