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
Tomcat base URL redirection
提问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.html
or index.jsp
of your default WAR file. Make the following changes in your webapp/ROOT
folder for redirecting requests to http://yourserver.example.com/somewhere/else
:
你可以这样做:如果你的 tomcat 安装是默认的并且你没有做任何更改,那么默认的 war 将是ROOT.war
. 因此,无论您何时调用http://yourserver.example.com/
,它都会调用您的默认 WAR 文件的index.html
或index.jsp
。在您的webapp/ROOT
文件夹中进行以下更改以将请求重定向到http://yourserver.example.com/somewhere/else
:
Open
webapp/ROOT/WEB-INF/web.xml
, remove any servlet mapping with path/index.html
or/index.jsp
, and save.Remove
webapp/ROOT/index.html
, if it exists.Create the file
webapp/ROOT/index.jsp
with 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"); %>
打开
webapp/ROOT/WEB-INF/web.xml
,删除带有路径/index.html
或 的任何 servlet 映射/index.jsp
,然后保存。删除
webapp/ROOT/index.html
,如果存在。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 ROOT
folder under your Tomcat's webapps
folder; you can then configure redirects to any other context within its WEB-INF/urlrewrite.xml
configuration file.
您需要将其解压缩到ROOT
Tomcatwebapps
文件夹下的文件夹中;然后,您可以在其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/