Process of permanent domain name change for jsp websites

Source: Internet
Author: User

1. Create a VM for the old domain name under tomcat
The homepage is index. jsp, And the 404 error page is unfind. jsp.
301 redirection on the home page, facilitating Search Engine Optimization
<%
Response. setStatus (301 );
Response. setHeader ("Location", "http: // ***. com ");
Response. setHeader ("Connection", "close ");
%>
Unfind. jsp determines the address of the new page to which the old page should be switched. If it is not, it will jump to the new page so that the previous External links will not be affected.

<% @ Page language = "java" contentType = "text/html; charset = UTF-8" isErrorPage = "true" pageEncoding = "UTF-8" %>
<%
Response. setStatus (HttpServletResponse. SC _ OK );
String key = (String) request. getAttribute ("javax. servlet. forward. servlet_path ");
String id = request. getParameter ("id ");
If (key! = Null ){
Int index = key. lastIndexOf ("/");
If (index! =-1 ){
Key = key. substring (index + 1 );
If (key. startsWith ("v ")){
Response. sendRedirect ("http: // ***. com/viewtv/" + id );
}
If (key. startsWith ("t ")){
Response. sendRedirect ("http: // ***. com/TV/" + id );
}
}
}
Response. sendRedirect ("http: // ***. com ");
%>
References
Post from http://blog.csdn.net/lifaming15/archive/2008/10/16/3084282.aspx
URL rewriting actually turns a url with a lot of parameters into a seemingly regular url for the search engine.
Example
/Viewthread. jsp? Id = 1234
/Viewthread. jsp? Id = 1235
/Viewthread. jsp? Id = 1236
After rewriting, you can use
/Viewthread/1234.htm
/Viewthread/1235.htm
/Viewthread/1236.htm
I currently use Tomcat + Apache and have tried three rewrite methods.
I. Tomcat Filters
The most typical class library is urlReweite. If you have experience, writing by yourself is not very troublesome. You can also use the ready-to-use open-source free urlrewrite filter. http://tuckey.org/urlrewrite/download
Modify web. xml to add a filter and configure a filter rule.
Web. xml modification Section
<! -- Set URL Rewrite -->
<Filter>
<Filter-name> UrlRewriteFilter </filter-name>
<Filter-class>
Org. tuckey. web. filters. urlrewrite. UrlRewriteFilter
</Filter-class>
</Filter>
<Filter-mapping>
<Filter-name> UrlRewriteFilter </filter-name>
<Url-pattern>/* </url-pattern>
</Filter-mapping>
Filter rules
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE urlrewrite PUBLIC "-// tuckey.org//DTD UrlRewrite 2.6 // EN"
Http://tuckey.org/res/dtds/urlrewrite2.6.dtd>
<Urlrewrite>
<Rule>
<From>/viewthread/(\ d00000000.htm $ </from>
<To>/viewthread. jsp? Id = $1 </to>
</Rule>
</Urlrewrite>
Ii. Use Apache mod
# Remove the preceding # and enable it
LoadModule rewrite_module modules/mod_rewrite.so
<VirtualHost _ default _: 80>
# Other configuration data
RewriteEngine On
# The following three lines implement dynamic resolution
RewriteRule ^/viewthread/(\ d1_0000.htm $/viewthread. jsp? Id = $1 [L, PT]
</VirtualHost>
3. Use System 404 errors
That is, the error not found on the page
Instance:
Http://www.xxx.net/f29
Will be automatically forwarded
Http://www.xxx.net/forumdisplay.jsp? Fid = 29
Because f29 does not exist in the system, the error handling page of 404 is used to implement this function.
The page code is as follows:
View copies to clipboard Printing
<% @ Page language = "java" contentType = "text/html; charset = GBK" isErrorPage = "true" pageEncoding = "GBK" %>
<%
Response. setStatus (HttpServletResponse. SC _ OK );
String key = (String) request. getAttribute ("javax. servlet. forward. servlet_path ");
If (key! = Null ){
Int index = key. lastIndexOf ("/");
If (index! =-1 ){
Key = key. substring (index + 1 );
If (key. startsWith ("f") | key. startsWith ("p ")){
Try {
Long id = Long. parseLong (key. substring (1 ));
String url = key. startsWith ("f ")? "Forumdisplay. jsp? Fid = ":" viewthread. jsp? Tid = ";
%>
<Jsp: forward page = "<% = url + id %>"> </jsp: forward>
<%
Return;
} Catch (Exception ex ){}
}
%>
<Jsp: forward page = "<% = response. encodeURL ("search. jsp ") %>"> <jsp: param name = "keyword" value = "<% = key %>"/> </jsp: forward>
<%
Return;
}
}
%>
Sorry, the page you requested is not found!
Summary
First, modification is troublesome, but it is a good choice for stable running systems.
The second type requires configuration of Apache, but the restart speed of Apache is very fast, suitable for situations that may often change, and the efficiency of Apache Mod is good.
The third type is the most flexible, which can be used by simple applications.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.