Java-urlrewrite Chinese API documentation

Source: Internet
Author: User
Tags expression engine

Installation

1. Download the jar package and add it to web-inf/lib

2. Add the following configuration to the Web-inf/web.xml

<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 -->

<url-pattern>/*</url-pattern>

<dispatcher>REQUEST</dispatcher>

<dispatcher>FORWARD</dispatcher>

</filter-mapping>

3. Add the configuration file under web-inf/urlrewrite.xml

4. Restart the context environment

Parameters of the filter

1. confReloadCheckInterval: Configure file reload interval. 0 means load at any time,-1 means no reload, default-1

2. confPath: configuration file path. Is the relative context of the path, the default/web-inf/urlrewrite.xml

3. logLevel: set the log level, which can be: TRACE, DEBUG, INFO (default), WARN, ERROR, FATAL, log4j, Commons, SLF4J, sysout:{level} (e.g. Sysout:debug), such as You have some difficulty using the normal log level, you can tune to: Sysout:debug (indicates the debug level using the console output)

4. statusPath: set change state path, cannot conflict with installed app (note, default is/rewrite-status), note, must start with/

5. statusEnabled: set whether status is turned on, expected value is true, False, default True

6. statusEnabledOnHosts: set the Allow status host, * Can be used as a wildcard character, default is "localhost, local, 127.0.0.1"

7. modRewriteConf: set rewrite mode, default is False, use Mod-rewrite (can refer to Apache server mod_rewrite related data) mode configuration file, If set to True and Confpath is not set then the profile path will be defaulted to/web-inf/.htaccess

8. modRewriteConfText: from the values of these parameters and from the configuration of the load mod_rewrite style, setting these parameters will be ignored for all other parameters. For example:

<init-param>

<param-name>modRewriteConfText</param-name>

<param-value>

RewriteRule ^/~([^/]+)/?(.*) /u/$1/$2 [R]

RewriteRule ^/([uge])/([^/]+)$ /$1/$2/ [R]

</param-value>

</init-param>

9. allowConfSwapViaHttp: set whether parameters are allowed to be interactively set via HTTP, for example, by calling/rewrite-status/?conf=web-inf/urlrewrite2.xml

Configuration file Web-inf/urlrewrite.xml

1. DTD constraints

<!DOCTYPE urlrewrite
        PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN"
        "http://tuckey.org/res/dtds/urlrewrite3.0.dtd">

2. <urlrewrite/>

2.1. Default-match-type (optional):

2.1.1. Regex, default. All rule that does not specify the Match-type attribute is matched using a Java regular expression

2.1.2. Wildcard: All rule that does not specify the Match-type attribute uses a wildcard match engine match

2.2. Decode-using (optional):

2.2.1. Header, UTF8: default. Use Request.getcharacterencoding () to get the encoding to decode the URL, and if it is empty, use UTF8.

2.2.2. Null: No decoding is performed. Set to: decode-using= "null"

2.2.3. Header: Use only request.getcharacterencoding () decoding

2.2.4. [Encoding]: Use only a specified character encoding such as iso-8859-1.

2.2.5. Header, [encoding]: When decoding a URL using request.getcharacterencoding (), if the resulting value is NULL, the encoding specified by encoding is used.

2.3. use-query-string (optional):

2.3.1. False: Default. When the from is matched, the query string does not participate in the

2.3.2. True: Query string participates in the from match

2.4. Use-context (optional):

2.4.1. False: Default. When the from element matches, the Contex path of the application will not be added to the URL

2.4.2. True:application's Contex path participates in the FROM element match

3. <rule/>: 0 or more

3.1. Enabled (optional):

3.1.1. True: Default. Allow this rule

3.1.2. False: Discard this rule

3.2. Match-type (optional):

3.2.1. Regex: Default. Using Java Regular matching

3.2.2. Wildcard: Using a wildcard expression engine

4. <outbound-ruld/>: 0 or more. Very similar to normal rule, but this is overridden when the Response.encodeurl () method is called.

4.1. Enabled (optional):

4.1.1. True: Default. Allow rules

4.1.2. False: Obsolete rules

4.2. Encodefirst (optional):

4.2.1. Fasle: By default, this rewrite rule is applied after the Encodeurl () method is run

4.2.2. True: Apply this rewrite rule before Encodeurl ()

5. <name/>: An optional element that records the name of the rule, which can be used on <rule/> and <outbound-rule/>

6. <note/>: A simple optional element for documenting rule descriptions that can be used on <rule/> and <outbound-rule/>.

7. <condition/>: Selection criteria for the rule. Note that all the conditions must be met when the rule is applied.

7.1. Type (optional):

7.1.1. Header: Default. If set, the header name must be specified by the Name property of the <condition/>

7.1.2. Method: Request methods. GET, POST, head, etc.

7.1.3. Port:application Running Port

7.1.4. Time: Current times of the server (using UNIX timestamps), which is often used to ensure that content survives only at set time

7.1.5. Year: The year before the server

7.1.6. Month: Current month of the server

7.1.7. DayOfMonth: The day of January, the first day of the month is 1

7.1.8. DayOfWeek: Day of the week, Sunday is 7

7.1.9. AMPM: Morning or afternoon

7.1.10. Hourofday: Number of hours of the day (24-hour system)

7.1.11. minute: Minute of current server time

7.1.12. Second: Seconds of current server time

7.1.13. Millisecond: Milliseconds for the current server time

7.1.14. Attribute: Check the request's property (GetAttribute) value to check the property name specified by <condition/> Name

7.1.15. Auth-type: Check the value of the request property. Request.getauthtype

7.1.16. Character-encoding: Received the requested encoding

7.1.17. Content-length: The length of the request (useful for rejecting a large request)

7.1.18. Content-type: Request type

7.1.19. Context-path: Contex Path of the request

7.1.20. Cookies: Check the cookie value, the name of the cookie is specified by <condition/> Name property

7.1.21. Parameter: Check request parameters, parameter names specified by <condition/> 's Name property

7.1.22. Path-info: Equivalent to Request.getpathinfo ()

7.1.23. path-translated: Equivalent to request.gettranslated ()

7.1.24. Protocol: For filtering protocols

7.1.25. query-string: Get the argument string following the URL

7.1.26. REMOTE-ADDR:IP Address filtering

7.1.27. Remote-host: Remote host filtering (note that only available if the application server is configured with a view (remote) host name)

7.1.28. Remote-user: Currently logged in user, if user is authorized to be available

7.1.29. Requested-session-id: ID of the current session

7.1.30. Request-uri: From the protocol name to the query string portion of the request URL

7.1.31. Request-url: A refactored URL that returns a URL that contains a protocol, server name, port, path, but does not contain a query string

7.1.32. Session-attribute: Examine the properties (GetAttribute) in the session, and the property name is set by the Name property of the <condition/>.

7.1.33. Session-isnew: Check if the session is new

7.1.34. Server-name: The host name of the server to which the request was sent (not the machine name from the host header)

7.1.35. Scheme: The requested scheme

7.1.36. User-in-role: Note that the value here cannot be a regular expression

7.2. Name: Used with some special type, can be any value

7.3. Next:

7.3.1. And: Default. The next and this condition must match

7.3.2. Or: Next or this condition matches

7.4. Operator:

7.4.1. Equal: Default. Specify regular and true value matching

7.4.2. NotEqual: True value and regular mismatch

7.4.3. Greater: greater than, only for numeric values

7.4.4. Less: smaller than

7.4.5. Greaterorequal: Greater than or equal to

7.4.6. Lessorequal: Less than or equal to

8. <from/>: Usually you must specify one in <rule/> and <outbound-rule/>, and the value can be regular expression (regular in PERL5 mode), note: The URL specified by the From IS related to Contex

8.1. CaseSensitive:

8.1.1. False: Default. Case insensitive

8.1.2. True: Case Sensitive

9. <to/>: Can be a perl5-style regular substitution expression

9.1. Type:

9.1.1. Forward: Default. The request matches all <condition/> of this <rule/>, and the URL uses an internal jump to the address specified by "to" (note that the URL forward here must be in the same container as Urlrewritefilter)

9.1.2. Passthrough: Same as forward

9.1.3. Redirect: request matches all <condition/> and <rule/> of this <from/>, notify client to jump to <to/> Specify address

9.1.4. Permanent-redirect: The equivalent of doing the following things

Response.setstatus (

httpservletresponse.sc_moved_permanently

);

Response.setheader ("Location", [<to/> Specified value]);

9.1.5. Temporary-redirect: The equivalent of doing the following things

Response.setstatus (

HttpServletResponse. Sc_moved_temporarily

);

Response.setheader ("Location", [<to/> Specified value]);

9.1.6. Pre-include

9.1.7. Post-include

9.1.8. Proxy: The request URL will be proxied in full path, using this feature requires the introduction of Commons-http and COMMONS-CODEC packages

9.2. Last:

9.2.1. False: Default. The rest of the <rule/> will be processed if this match

9.2.2. True: If matching this rule will not process

9.3. Encode:

9.3.1. False: <rule/> is the default value. Before rewrite, encode the URL with the Response.encodeurl ([to] value)

9.3.2. True: <outbound-rule/> The default value. URL not encoded

9.4. Context:

If the application server is configured to allow "penetrate context" communication, then this property can be used for forward (and can only be used for forward) requests to another Serlvet context .... That is, cross-application forward

On Tomcat, configure crosscontext= "true" in Server.xml or context.xml, for example: Allow two apps to communicate between "app" and "forum", then configure as follows:

<context docbase= "App" Path= "/app" reloadable= "true" crosscontext= "true"/>

<context docbase= "forum" path= "/forum" reloadable= "true" crosscontext= "true"/>

Other aspects of <to/>

10.1. <to/> can be null, meaning: If the match request no longer continues, it is equivalent to not calling Chain.dofilter

10.2. Using $n to get the subgroups configured in the <from/>, n must be a number from 1 to 10

10.3. The values in the type that can be used in any <condition/> can be used in <to/>, such as <to>/%{parameter:page}</to>

10.4. Function call: ${function Name: Parameter 1: Parameter 2} can be used in <set/> and <to/>

Name

Example

Example returns

Replace

${replace:my Cat is a blue Cat:cat:dog}

My dog is a blue dog

Replacefirst

${replace:my Cat is a blue Cat:cat:dog}

My cat is a blue dog

Escape

${escape:a b C}

A+b+c

Unescape

${UNESCAPE:A+B+C}

A b C

Lower

${lower:hello World}

Hello World

Upper

${upper:hello}

HELLO

Trim

${trim:abc def}

ABC def

<set/>: When matching rules, you are allowed to set some values.

11.1. Type:

11.1.1. Request: Default. Similar to Request.setattribute

11.1.2. Session:session.setAttribute

11.1.3. Response-header:response.setheader

11.1.4. Cookies: Values are formatted with "[value][:d omain[:lifetime[:p Ath]]". is to set a cookie to the client browser, which is specified by the Name property of the <set/>

11.1.4.1. Value of Value:cookie

11.1.4.2. Domain: Server

11.1.4.3. Lifetime: Inventory time

11.1.4.4. Path to Path:cookie

11.1.5. Status:response.setStatus

11.1.6. Content-type:response.setcontenttype

11.1.7. charset:response.setCharacterEncoding

11.1.8. Expires: Sets the expiration time in the HTTP header, formatted as {numeric type}, for example: "1 day 2 Seconds"

11.1.9. Locale:response.setLocale

11.1.10. Parameter: Allows the value of a parameter obtained by Request.getparameter to be re-processed here

11.1.11. Method: Allows the value of Request.getmethod () to be re-processed

11.2. Name:type is the request, session, Response-header, cookie, you must set the name

11.3. For example:

<rule>

<condition name= "User-agent" >mozilla/3/.0 (compatible; AvantGo. *) </condition>

<from>.*</from>

<set name= "Client" >AvantGo</set>

</rule>

<rule>

<condition name= "User-agent" >up/. BROWSER/3.*SC03 .*</condition>

<from>.*</from>

<set name= "Client" >samsung sch-6100</set>

</rule>

<run/>: Allows an object method to be executed when <rule/> and <condition/> are matched

12.1. Class: The class name of the fully qualified name, which expects the class name of the method to be called.

12.2. Method (optional): The default value is run. The name of the method to expect to invoke.  The method must have two parameters (HttpServletRequest request, httpservletresponse response). Note that if the object has an init (servletconfig) or Destroy () method, it is automatically called when the object is created and destroyed, and ServletConfig can be initialized with parameters passed through <init-param/> :

<run class= "SELFIMPR. Myservlet "method=" Doget ">

<init-param>

<param-name>id</param-name>

<param-value>1</param-value>

</init-param>

</run>

12.3. Neweachtime: False by default. Indicates whether an object instance is created each time the request is made.

Tip

13.1. In the configuration, if you want to use "&", use &amp;

13.2. For simplicity, add ^, $, to the <from/> configuration before and after, both of which are mandatory start and end flags in regular expressions

13.3. If you use <outbound-rule> remember that the URL in the code is encoded

13.4. The regular expression is very complex and flexible, please read the Java regular introduction in Java.util.regex.Pattern

13.5. If you feel that it is difficult to understand, you can use a wildcard method

13.6. Contex is very important, if there is an application context is "/myapp", and your request is "/myapp/somefolder/somepage.jsp", the container gives Urlrewritefilter URL will be " /somefolder/somepage.jsp ", this may be difficult to understand, but in your <rule> and <condition> do not include the context path, it is the container responsible for processing.

14. Wildcard Characters:

Wildcard matching engines can replace regular expressions, and setting Match-type in <condition> and <rule> is wildcard to turn on support wildcard characters. (or set Default-match-type)

For example:

/big/url/* matches/big/url/abc.html but does not match/big/url/abc/dir/or/big/url/abc/

/big/url/** matches/big/url/abc.html,/big/url/abc/dir/and/big/url/abc/

can also be replaced with regular, each * represents a parameter, in <set> and <to> using $n way

Configuring the filter configuration using the Mod-rewrite style

<filter>

<filter-name>UrlRewriteFilter</filter-name>

<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>

<!--defaults to False. Use mod_rewrite style configuration file (if the is true and Confpath

is not specified Confpath would be set to/web-inf/.htaccess)-

<init-param>

<param-name>modRewriteConfText</param-name>

<param-value><! [cdata[

# REDIRECT Mozilla to another area

Rewritecond%{http_user_agent} ^mozilla.*

Rewriterule ^/no-moz-here$/homepage.max.html [L]

]]></param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>UrlRewriteFilter</filter-name>

<url-pattern>/*</url-pattern>

<dispatcher>REQUEST</dispatcher>

<dispatcher>FORWARD</dispatcher>

</filter-mapping>

Specific matching configurations under Web-inf/.htaccess

# REDIRECT Mozilla to another area

Rewritecond%{http_user_agent} ^mozilla.*

Rewriterule ^/no-moz-here$/homepage.max.html [L]

URL annotation Matching

1. After urlrewrite3.0, you can use annotations to generate Urlrewrite configuration files using JDK1.6 and above.

Java-urlrewrite Chinese API documentation

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.