Link to home
Start Free TrialLog in
Avatar of chandesh sanjay
chandesh sanjay

asked on

301 redirect in tomcat server

Hi, anyone explain me how to do 301 redirect in tomcat server, my problem is i have a index.html file in root folder of tomcat server, where this index.html file is forwarded to another folder root/xx/ui/ folder where my original index.html file is present, so now i want to 301 redirect the root insex.html to ui/index.html file. Please anyone help me.
Avatar of Anthony Garcia
Anthony Garcia
Flag of United States of America image

Are you sure you want to do a redirect and not a rewrite rule? See this blog post for the difference.
https://blogs.iis.net/owscott/url-rewrite-vs-redirect-what-s-the-difference.

What version of tomcat are you using. I think past tomcat 8 you can use a rewrite valve. You can enable it by adding it to the context.xml file, so it looks something like this.
<?xml version="1.0" encoding="UTF-8"?>
<!-- The contents of this file will be loaded for each web application -->
<Context>
    <!-- Add This rewrite valve -->
    <Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />
    
    <!-- Default set of monitored resources. If one of these changes, the    -->
    <!-- web application will be reloaded.                                   -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
</Context>

Open in new window


Then you need to create a file called rewrite.config and put it in the WEB-INF directory.
This will have you rewrite rule which can also set redirect codes.
Inside of the file you can put the following rewrite rule
RewriteRule ^index.html$ ui/index.html [R=301,L]

Open in new window

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.