Html Tomcat 基础 URL 重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1363605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:47:14  来源:igfitidea点击:

Tomcat base URL redirection

htmltomcat

提问by Nathaniel Flath

Using tomcat, how do I get a request for http://www.mydomain.comto redirect to http://www.mydomain.com/somethingelse/index.jsp? i haven't even managed to get an index.html to display from http://mydomain.com.

使用 tomcat,我如何获得http://www.mydomain.com重定向到http://www.mydomain.com/somethingelse/index.jsp的请求?我什至还没有设法从http://mydomain.com获得一个 index.html 来显示。

采纳答案by flybywire

Name your webapp WAR “ROOT.war” or containing folder “ROOT”

将您的 webapp WAR 命名为“ROOT.war”或包含文件夹“ROOT”

回答by Viral Patel

You can do this: If your tomcat installation is default and you have not done any changes, then the default war will be ROOT.war. Thus whenever you will call http://yourserver.example.com/, it will call the index.htmlor index.jspof your default WAR file. Make the following changes in your webapp/ROOTfolder for redirecting requests to http://yourserver.example.com/somewhere/else:

你可以这样做:如果你的 tomcat 安装是默认的并且你没有做任何更改,那么默认的 war 将是ROOT.war. 因此,无论您何时调用http://yourserver.example.com/,它都会调用您的默认 WAR 文件的index.htmlindex.jsp。在您的webapp/ROOT文件夹中进行以下更改以将请求重定向到http://yourserver.example.com/somewhere/else

  1. Open webapp/ROOT/WEB-INF/web.xml, remove any servlet mapping with path /index.htmlor /index.jsp, and save.

  2. Remove webapp/ROOT/index.html, if it exists.

  3. Create the file webapp/ROOT/index.jspwith this line of content:

    <% response.sendRedirect("/some/where"); %>
    

    or if you want to direct to a different server,

    <% response.sendRedirect("http://otherserver.example.com/some/where"); %>
    
  1. 打开webapp/ROOT/WEB-INF/web.xml,删除带有路径/index.html或 的任何 servlet 映射/index.jsp,然后保存。

  2. 删除webapp/ROOT/index.html,如果存在。

  3. webapp/ROOT/index.jsp使用以下内容创建文件:

    <% response.sendRedirect("/some/where"); %>
    

    或者如果您想定向到不同的服务器,

    <% response.sendRedirect("http://otherserver.example.com/some/where"); %>
    

That's it.

就是这样。

回答by ChssPly76

Take a look at UrlRewriteFilterwhich is essentially a java-based implementation of Apache's mod_rewrite.

看看UrlRewriteFilter,它本质上是 Apache 的 mod_rewrite 的基于 Java 的实现。

You'll need to extract it into ROOTfolder under your Tomcat's webappsfolder; you can then configure redirects to any other context within its WEB-INF/urlrewrite.xmlconfiguration file.

您需要将其解压缩到ROOTTomcatwebapps文件夹下的文件夹中;然后,您可以在其WEB-INF/urlrewrite.xml配置文件中配置重定向到任何其他上下文。

回答by obaid

Tested and Working procedure:

测试和工作程序:

Goto the file path ..\apache-tomcat-7.0.x\webapps\ROOT\index.jsp

转到文件路径 ..\apache-tomcat-7.0.x\webapps\ROOT\index.jsp

remove the whole content or declare the below lines of code at the top of the index.jsp

删除整个内容或在 index.jsp 顶部声明以下代码行

<% response.sendRedirect("http://yourRedirectionURL"); %>

<% response.sendRedirect("http://yourRedirectionURL"); %>

Please note that in jsp file you need to start the above line with <% and end with %>

请注意,在jsp文件中你需要以<%开头并以%>结尾

回答by Kevin

What i did:

我做了什么:

I added the following line inside of ROOT/index.jsp

我在 ROOT/index.jsp 中添加了以下行

 <meta http-equiv="refresh" content="0;url=/somethingelse/index.jsp"/>

回答by progressdll

In Tomcat 8 you can also use the rewrite-valve

在 Tomcat 8 中,您还可以使用 rewrite-valve

RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^/(.*)$         /somethingelse/index.jsp

To setup the rewrite-valve look here:

要设置重写阀,请看这里:

http://tonyjunkes.com/blog/a-brief-look-at-the-rewrite-valve-in-tomcat-8/

http://tonyjunkes.com/blog/a-brief-look-at-the-rewrite-valve-in-tomcat-8/