Sunday, August 17, 2014

Perfofming SEO friendly site redirection on a java app server to another domain

Hello everybody,

Today I will analyze a very simple and successful way to redirect you old domain hosted in a java app server (Tomcat for our example) to your new domain so that search engines understand that the old domain does not exist anymore and has moved to a new location (SEO friendly : Search Engine Optimizations friendly). All you need to do is set up your old domain to respond with an http status code 301 that means "Moved Permanently" for any request.

First, in the root directory of your old domain create a jsp with the name redirect.jsp containing the following code :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%
response.setStatus(301);
response.setHeader( "Location", "http://www.NEW-DOMAIN.com");
response.setHeader( "Connection", "close" );
%>

Replace "http://www.NEW-DOMAIN.com" with the url of your new domain.

Next add the following lines inside the root directory's web.xml (for Tomcat installations inside webapps/ROOT/WEB-INF/web.xml):


<servlet>
   <servlet-name>redirectorrr</servlet-name>
   <jsp-file>/redirect.jsp</jsp-file>
</servlet>
<servlet-mapping>
   <servlet-name>redirectorrr</servlet-name>
   <url-pattern>/*</url-pattern>
</servlet-mapping>

And we're done. You may notice that the servlet name is totally irrelevant, so you can change it to anything you like (change it in both lines to a new servlet name).



p.s.
You can find me on fiverr for more personalized requests on any java app server configuration problem or java error that you encounter, with deliverance of less than a day (true!) and money back guarantee if not satisfied.