找不到页面时更改 tomcat 的行为 (404)
change behavior of tomcat when page not found (404)
任何人都可以就如何配置 Tomcat 提出建议,以便完全避免 404,而不是在找不到页面时(特别是在应该触发 404 的情况下)return index.html 内容和 200 作为状态?
让我告诉你我为什么要这样做:
我们有 angular 应用程序 "www.app.exmpl/app",当用户点击 "www.app.exmpl/app" 应用程序时,应用程序会正确显示和 200 returned。 index.html returned 其中有 angular 应用程序代码。
当用户点击 "www.app.exmpl/app/some_unexisted_url_in_file_system_path" 时 tomcat 试图找到端点或 html 可以服务于此 url 的文件,并且 return s 404,在web.xml中我们有这样的配置:
<error-page>
<error-code>404</error-code>
<location>/index.html</location>
</error-page>
这会导致用户收到 404 响应,但网络应用程序仍会加载
正确,因为 index.html 中有 angular 应用程序并且加载页面后
将 url 重写为正确的,例如:"www.app.exmpl/app".
- 当用户点击 "www.app.exmpl/app/url_handled_by_angular_app" 时,仍然找不到 "url_handled_by_angular_app" 下的页面或端点,因此 tomcat 将代码设置为 404 并呈现 index.html,这会返回对用户而言,在用户的电脑上,此响应呈现为 angular 应用程序,无需 url 重写,因为 angular 应用程序知道如何处理此 "url_handled_by_angular_app" 路由。
所以总而言之,我只需要摆脱 404 并将它们全部替换为 200,并且在这种情况下只渲染 index.html 内容。这种情况的最佳做法是什么?
您能否提供有关 Routing Strategy
的更多详细信息。
好吧,我想你必须使用这个 {useHash: true}
这可能是一种情况,如果您还没有使用 {useHash: true}
并且当您重新加载它时搜索 URL 路径中的特定文件,
你可以在这里找到更多 https://thinkster.io/tutorials/angular-2-routing-navigation
当错误页面不是静态页面,而是 JSP 页面(或 Servlet)时,它可以更改响应状态 (response.setStatus(200)
) 并提供静态文件(转发给它或包括它)。
虽然我猜想在像您这样的用例中使用 302
(客户端重定向)响应更合适,例如 response.sendRedirect(application.getContextPath() + "/index.html")
.
如果服务器上没有重定向规则,应用程序深层链接将无法工作。所有深层链接都必须由服务器重定向到应用程序 index.html
。
设置 tomcat 重定向任何深层链接 -
- 在 server.xml
中配置 RewriteValve
- 在rewrite.config
中写入重写规则
1.在 server.xml
中配置 RewriteValve
编辑 ~/conf/server.xml 以在主机部分添加以下阀门,如下所示 –
...
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />
...
</Host>
...
2。在rewrite.config
中写入重写规则
创建目录结构 - ~/conf/Catalina/localhost/ 并在其中创建 rewrite.config 文件,内容如下。注意 - 在这里我考虑 /app
作为问题中提到的应用程序的上下文路径。
RewriteCond %{REQUEST_PATH} !-f
RewriteRule ^/app/(.*) /app/index.html
设置后重新启动 tomcat 服务器,您可以点击应用程序的深层链接,该链接将路由到 angular 应用程序内的正确组件。
详情可参考this post
任何人都可以就如何配置 Tomcat 提出建议,以便完全避免 404,而不是在找不到页面时(特别是在应该触发 404 的情况下)return index.html 内容和 200 作为状态? 让我告诉你我为什么要这样做:
我们有 angular 应用程序 "www.app.exmpl/app",当用户点击 "www.app.exmpl/app" 应用程序时,应用程序会正确显示和 200 returned。 index.html returned 其中有 angular 应用程序代码。
当用户点击 "www.app.exmpl/app/some_unexisted_url_in_file_system_path" 时 tomcat 试图找到端点或 html 可以服务于此 url 的文件,并且 return s 404,在web.xml中我们有这样的配置:
<error-page> <error-code>404</error-code> <location>/index.html</location> </error-page>
这会导致用户收到 404 响应,但网络应用程序仍会加载 正确,因为 index.html 中有 angular 应用程序并且加载页面后 将 url 重写为正确的,例如:"www.app.exmpl/app".
- 当用户点击 "www.app.exmpl/app/url_handled_by_angular_app" 时,仍然找不到 "url_handled_by_angular_app" 下的页面或端点,因此 tomcat 将代码设置为 404 并呈现 index.html,这会返回对用户而言,在用户的电脑上,此响应呈现为 angular 应用程序,无需 url 重写,因为 angular 应用程序知道如何处理此 "url_handled_by_angular_app" 路由。
所以总而言之,我只需要摆脱 404 并将它们全部替换为 200,并且在这种情况下只渲染 index.html 内容。这种情况的最佳做法是什么?
您能否提供有关 Routing Strategy
的更多详细信息。
好吧,我想你必须使用这个 {useHash: true}
这可能是一种情况,如果您还没有使用 {useHash: true}
并且当您重新加载它时搜索 URL 路径中的特定文件,
你可以在这里找到更多 https://thinkster.io/tutorials/angular-2-routing-navigation
当错误页面不是静态页面,而是 JSP 页面(或 Servlet)时,它可以更改响应状态 (response.setStatus(200)
) 并提供静态文件(转发给它或包括它)。
虽然我猜想在像您这样的用例中使用 302
(客户端重定向)响应更合适,例如 response.sendRedirect(application.getContextPath() + "/index.html")
.
如果服务器上没有重定向规则,应用程序深层链接将无法工作。所有深层链接都必须由服务器重定向到应用程序 index.html
。
设置 tomcat 重定向任何深层链接 -
- 在 server.xml 中配置 RewriteValve
- 在rewrite.config 中写入重写规则
1.在 server.xml
中配置 RewriteValve编辑 ~/conf/server.xml 以在主机部分添加以下阀门,如下所示 –
...
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />
...
</Host>
...
2。在rewrite.config
中写入重写规则创建目录结构 - ~/conf/Catalina/localhost/ 并在其中创建 rewrite.config 文件,内容如下。注意 - 在这里我考虑 /app
作为问题中提到的应用程序的上下文路径。
RewriteCond %{REQUEST_PATH} !-f
RewriteRule ^/app/(.*) /app/index.html
设置后重新启动 tomcat 服务器,您可以点击应用程序的深层链接,该链接将路由到 angular 应用程序内的正确组件。
详情可参考this post