Tomcat URL Redirect

Problem

Needed to re-direct a user to the default Tomcat web app when they select the base URL e.g when a user enters http://www.homeurl.org they are redirected to http://www.homeurl.org/name-of-webapp

Solution

Simple solution was to add the following line:

<% response.sendRedirect("/name-of-webapp"); %>

This was added to the index.jsp page in /usr/local/tomcat/webapps/ROOT.

In the example below, the above was added to line 18:

<%--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--%>
<!DOCTYPE html>
<% response.sendRedirect("/name-of-webapp"); %>
<%@ page session="false" %>
<%
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy");
request.setAttribute("year", sdf.format(new java.util.Date()));
request.setAttribute("tomcatUrl", "http://tomcat.apache.org/");
request.setAttribute("tomcatDocUrl", "/docs/");
request.setAttribute("tomcatExamplesUrl", "/examples/");
%>

The best solution would be to use UrlRewriteFilter I havent tested this out yet though. The above was simple enough for my needs.

NOTE

Incase you are running tomcat from a docker instance then, login to the docker container shell and then add the above line as instructed above:


root@server:/root/# docker exec -it name-of-docker-instance sh
# vim /usr/local/tomcat/webapps/ROOT/index.jsp

Update – 2017-10-31

Check Tomcat Home:

To check which directory tomcat is running from use:

root@d6eeff07df38:/usr/local/tomcat# ps -ef | grep tomcat
root 6 1 0 Oct27 ? 00:08:21 /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/tomcat/endorsed -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar –Dcatalina.base=/usr/local/tomcatDcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
root 17689 17667 0 11:33 ? 00:00:00 grep tomcat
root@d6eeff07df38:/usr/local/tomcat#

Resources

Tomcat base URL redirection

UrlRewriteFilter

UrlRewriteFilter 4.0.3

301 permanent redirect with Tomcat HOWTO

How do I override the default home page loaded by Tomcat?

Where is tomcat home