Java UrlRewriter pseudo static technology application in depth analysis

  • 2020-04-01 01:26:29
  • OfStack

In general, we generate static pages of article content in order to better relieve server stress and enhance the friendliness of search engines.
But sometimes in order to be able to display some information in real time, or also want to use dynamic script to solve some problems, can not use static way to display the content of the site, must use dynamic page display.

In this way, the loss of search engine friendly, how to find a middle way between the two, how to enhance the readability of your site address and let the search engine quickly included in your site?

This requires you to beautify the address of your web page, which results in a pseudo-static technique known as Url Rewriter rewriting. When we visit a page, the address bar displays a static page ending in ".html "when we are actually visiting a dynamic page. The UrlRewriter technique is needed here.

You may be familiar with a lot of servers that provide Url rewriting technology, before we used the most is Apache, Jboss and other servers with some Url rewriting, but their configuration is more troublesome, performance is not very good. Now that we have a dedicated open source framework for Url rewriting, today I'm going to talk about UrlRewriteFilter, which is easy to use. UrlRewriteFilter is a Web filter for rewriting urls, similar to Apache's mod_rewrite. Suitable for any Web application server (Resin, Orion, Tomcat, etc.). Its typical application is the dynamic URL static, easy to search engine crawlers crawl your dynamic web pages.
Let's start with a quick look at how using Url rewriting can benefit your site.

The first: Is good for the search engine crawl, because now most of the search engines for dynamic page crawl is still relatively weak, they prefer to crawl some static pages. Most of the data on our current page is displayed dynamically. This requires us to dynamic page into a static page, conducive to the search engine crawl.

The second: Make it easier for users to understand, few users care about the address of the page on your site, but it is necessary to enhance the readability of large and medium-sized websites in general. This will make your site more perfect.

The third: Hidden technology, we can achieve the hidden technology through Url rewriting. Don't expose the technology you're using and make it easier for hobbyists who want to attack your site.

Fourth: Can be very convenient reuse, improve the portability of the site. If we change the background method, we can guarantee that the front page will not change. This improves the portability of the site.

Although it has so many advantages, but also has a little disadvantage, because it is implemented through the filter principle, it means that another access, how much will affect the point access speed, this can be ignored.
UrlRewriter technology now has two technical platforms, one in the Java direction and the other in the.net direction. Today we're talking about applications in the Java direction.

First, let's see how it works. It's simply a Filter. If you look at the source code, you'll quickly see that it's the forward() and sendRedirect() that we use in JSP.
The following is a simple application of Url rewriting technology:
The first step : download the urlrewrite-3.2.0 beta-jar and copy the urlrewrite-3.2.0 beta-jar to the classpath.
The second step : create a urlrewrite. XML configuration file in the web-inf directory.
The third step : initializes the configuration UrlRewriteFilter in the web.xml configuration file. Add the following code to the configuration file:
 
<!--  Set in the configuration file  URL Rewrite--> 
<FILTER></FILTER> 
<FILTER-NAME></FILTER-NAME>UrlRewriteFilter 
<FILTER-CLASS></FILTER-CLASS> 
org.tuckey.web.filters.urlrewrite.UrlRewriteFilter 
<FILTER-MAPPING></FILTER-MAPPING> 
<FILTER-NAME></FILTER-NAME>UrlRewriteFilter 
<URL-PATTERN></URL-PATTERN>/* 
<DISPATCHER></DISPATCHER>REQUEST 
<DISPATCHER></DISPATCHER>FORWARD 

Finally, I will briefly cover two common configuration rules, and the following is a simple urlrewrite.xml configuration snippet. Don't get used to the Java nomenclature of writing it as urlrewritt.xml so that you can add it
 
<INIT-PARAM></INIT-PARAM> 
<PARAM-NAME></PARAM-NAME>confPath 
<PARAM-VALUE></PARAM-VALUE>/WEB-INF/urlRewrite.xml 
 Errors are still reported when the server is started, because the source code must be all lowercase ( urlrewrite.xml ) and can only put WEB-INF The following.  
<!--l version="1.0" encoding="utf-8--> 
<URLREWRITE></URLREWRITE> 
<!--  Display topic posts  --> 
<RULE></RULE> 
<FROM></FROM>^/forum/thread/([0-9]+).html$ 
<TO type="forward"></TO>/forum/list.action?id=$1 
<RULE></RULE> 
<FROM></FROM>^/forum/thread/([0-9]+).html?page=([0-9]+)$ 
<TO type="forward"></TO>/forum/list.action?id=$1&page=$2 

All the rule configurations are written here. The first common rule is simple rewrite on the site.
< Rule>
< From> < / from>
< To type = "forward> < / to>
< / rule>
< From> < / from>
Write your own access address, usually in the form of a regular expression. < To type = "forward> < / to> That's the actual access address. Such as our actual access address is: http://www.phome.asia/forum/list.action? Id = 16931 & amp; Page = 2 and we want to rewrite it to http://www.phome.asia/forum/thread/16931.html? Page = 2. It looks better than we actually do. We should write it like this:
 
<RULE></RULE> 
<FROM></FROM>^/forum/thread/([0-9]+).html?page=([0-9]+)$ 
<TO type="forward"></TO>/forum/list.action?id=$1&page=$2 

A brief introduction to the usual normal expressions :
Code instructions
Matches any character other than a newline
\w matches letters or Numbers or underscores or Chinese characters
\s matches any blank character
\d match Numbers
\b matches the beginning or end of a word
^ matches the beginning of the string
$matches the end of the string
Commonly used & to use & To represent. $1,$2 represents configuring normal expressions with you > /(\w+)/(\w+)/ the corresponding parameter. < To type = "forward" > The default is type="forward".
Another common rule is to connect to external sites. You need to use < To type = "redirect" > .
 
<RULE></RULE> 
<FROM></FROM>^/rss/yahoo.html$ 
<TO type="redirect"></TO> http://add.my.yahoo.com/rss? url= http://feed.feedsky.com/ MySiteFeed

Related articles: