Let us assume we face a scenario where we move from a old domain (website) to a new domain (website). You may do this when you expand your business offering and the old name may not be appropriate.
But your old domain has gained reputation (like a good page rank and a number of incoming links) which you wish to carry forward to your new domain. One way of doing this is respond with HTTP code 301 which is permanently moved.
You may also make use of use Google web master tools to inform the change in domain address (as Google is currently the #1 search engine ). This will prevent the decrease in hits to your website from search engines.
The below code shows how to implement it if you host a Java web application for your old domain.
In web.xml make the below entry.
01 | xml version = "1.0" encoding = "UTF-8" ?>
|
04 | < servlet-name >RedirectionServlet servlet-name > |
05 | < servlet-class >com.olddomain.web.servlet.RedirectionServlet servlet-class > |
08 | < servlet-name >RedirectionServlet servlet-name > |
09 | < url-pattern >/* url-pattern > |
Next the code for RedirectionServlet.java
01 | package com.olddomain.web.servlet; |
03 | import java.io.IOException; |
04 | import javax.servlet.ServletException; |
05 | import javax.servlet.http.HttpServlet; |
06 | import javax.servlet.http.HttpServletRequest; |
07 | import javax.servlet.http.HttpServletResponse; |
09 | public class RedirectionServlet extends HttpServlet { |
11 | protected void processRequest(HttpServletRequest request, HttpServletResponse response) |
12 | throws ServletException, IOException { |
16 | response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); |
20 | protected void doGet(HttpServletRequest request, HttpServletResponse response) |
21 | throws ServletException, IOException { |
22 | processRequest(request, response); |
26 | protected void doPost(HttpServletRequest request, HttpServletResponse response) |
27 | throws ServletException, IOException { |
28 | processRequest(request, response); |
Note: Using response.sendRedirect will produce only HTTP status code 302 which is temporarily moved and not 301 which indicates it is permanently moved.
1 comment:
Good post and this enter helped me alot in my college assignement. Say thank you you seeking your information.
Post a Comment