URL address Rewriting

Source: Internet
Author: User

Now most of the site and the mall will use the URL rewrite, access to this, but also because the e-commerce mall is being done. URL rewriting, which is to display the original URL in another rule, makes it easy for users to access and also block some information.

Here is the benefit, in the development process, often encounter some with a lot of parameters of the URL, this way, on the one hand appears upset, on the other hand, some information directly on the URL, there will be some security issues. URL rewriting allows URLs with parameters to be represented in a more regular manner, such as:

/demoaction?id=1 ==>/demo1.html

It also hides the parameters that should be displayed on the URL, hiding the technical implementation and sensitive information. In addition, URL rewriting is also helpful for search engine access.

The URL rewrite recently contacted by the project is Urlrewrite, which uses the filter technology to implement the override by processing the URL that is accessed when the user requests it.

The following is an example of the use of Urlrewrite (personally, Urlrewrite's official documents are written or more fully understandable)

Import of Urlrewrite:

Urlrewrite import is very simple, first you need to add the Urlrewrite-3.2.0.jar package in the project's Lib folder, and then, in Web. XML, declare the filter

1 <filter> 2         <filter-name>UrlRewriteFilter</filter-name> 3         <filter-class> Org.tuckey.web.filters.urlrewrite.urlrewritefilter</filter-class> 4     </filter> 5     < Filter-mapping> 6         <filter-name>UrlRewriteFilter</filter-name> 7         <url-pattern>/*< /url-pattern> 8         <dispatcher>REQUEST</dispatcher> 9         <dispatcher>forward</ Dispatcher>10     </filter-mapping>11     

After you declare the filter, you need to create a new Urlrewrite.xml file in the Web-inf directory

1 <?xml version= "1.0" encoding= "Utf-8"? >2 <! DOCTYPE urlrewrite3 Public "-//tuckey.org//dtd urlrewrite 3.0//en" 4 "HTTP://TUCKEY.ORG/RES/DTDS/URLREWRITE3.0.DTD" >5 <urlrewrite>6     7     8 </urlrewrite>

The file is a rule-making file for Urlrewrite, followed primarily by configuring it for URL rewriting.

At this point, Urlrewrite's import is complete.

Urlrewrite after the successful import, the next mainly by adding rules in Urlrewrite.xml, you can implement the rewrite of the URL. Some common rules are listed here.

1 <rule>2         <from>^/demo/(\w+). html$</from>3         <to type= "redirect" >/struts/$1</to >4 </rule>

Rule is a child node under the Urlrewrite, which is the primary rule node of Urlrewrite, which contains the from and to two child nodes, from which represents the actual URL to which the requested url,to represents to go. There are two matching patterns for From,urlrewrite, one is regular expression matching, one wildcard match, and the other is regular expression matching. When a match is made, the regular of the matched part can be extracted as a parameter for delivery.

As on the rule set, when the client accesses the URL is http://127.0.0.1:8080/Struts/demo/hello.html, because the matching part is Hello, so it jumps to the http://127.0.0.1:8080/ On the Struts/hello. When there are more than one regular in the URL rule, the matching parameters are incremented as well. Such as:

1 <rule>2         <from>^/demo1/(\w+)/(\w+). html$</from>3         <to type= "redirect" >/struts/$ 1.action?age=$2</to>4 </rule>

Rule's default match is a regular expression, but sometimes it can be matched in the form of a wildcard character. When writing a rule, simply add a match-type= "wildcard" attribute to rule.

1 <rule match-type= "wildcard" >2         <from>/demo2/*/*</from>3         <to type= "redirect" >/ Struts/$1.action?age=$2</to>4 </rule>

With regard to the to node, Urlrewrite offers a variety of URL jumps, such as forward and redirect, both of which are not discussed in the same way as most of the MVC framework provides.

In addition to the jump that supports the specified rule, Urlrewrite also supports a function that executes an object when the rule is matched

1 <rule>2         <from>^/demo3/(\w+)/(\w+). html$</from>3         <run class= "Com.sean.action.Demo" method= "Log"/>4         <to type= "redirect" >/struts/$1.action?age=$2</to>5 </rule>

As the above setting, to implement a matching rule is to execute a function, you need to add more than one run node, add the corresponding class properties and method properties on the node. At the same time, the corresponding class must inherit the Rewriterule class, and the executed method must pass in two parameters, namely HttpServletRequest and HttpServletResponse

1 public class Demo extends rewriterule{2  3 public     void log (HttpServletRequest request,httpservletresponse Response) {4         System.out.println ("Haha1"); 5     } 6      7 public     void log2 (HttpServletRequest request, HttpServletResponse response) {8         System.out.println ("HAHA2"); 9     }10}

Thus, when the URL entered by the client is first matched to the specified rule, urlrewrite executes the corresponding function, which executes only when the first match succeeds.

If you want to execute a function every time the match rule is executed, you can add the Class-rule child node in Urlrewrite, and after the node is set, the specified function executes once each time the rule is matched.

1 <class-rule class= "Com.sean.action.Demo" method= "log2"/>

In addition to processing the requested URL, Urlrewrite provides the ability to override the address in the returned page. The use of rule is to process the URL entered by the user, but during development, it is often necessary to add some URL requests to the page, and Urlrewrite can rewrite the URLs in the page through rules. Such as:

1 <outbound-rule match-type= "regex" >2         <from>/(\w+). action\?id= (\w+) $</from>3         <to >/$1.html</to>4 </outbound-rule>

After adding the rule in urlrewrite, we have the original address in the page

However, the display on the page is as follows:

This can hide a lot of the technology in development, more secure.

These are some of the common uses of urlrewrite. About Urlrewrite, some people on the internet say that this will affect performance, because in each request is required to undergo a change of filter, but this is still a matter of opinion, after all, the use of URL rewrite, the URL is still good.

Urlrewrite's Help document: Http://pan.baidu.com/s/1c0fg0uc

URL address Rewriting

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.