UrlRewrite overrides url for details and examples

  • 2020-05-19 04:52:08
  • OfStack

UrlRewrite rewrite url in detail

UrlRewrite is what we call address rewriting, and all the user gets is the processed URL address.

As the name implies, urlrewrite is to rewrite URL. All the users get is the URL address after processing. In my opinion, there are three advantages in this way:

1: improve security, can effectively avoid 1 parameter name, ID completely exposed in front of the user, if the user randomly type, do not comply with the rules will directly return a 404 or error page, which is better than returning 500 or 1 lot of server error information

2: beautify URL, remove those suffixes such as *.do, long parameter strings, etc., can organize and simplify URL, which can better reflect the content of the access module

3. It is more conducive to the income of search engines. Through some optimization of URL, search engines can better identify and collect the information of websites

Using the step

1 download jar

Official address: http: / / tuckey org/urlrewrite /
Download urlrewritefilter-4.0.3.jar and join the project lib directory.

2 configuration 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-pattern>/*</url-pattern>
  <dispatcher>REQUEST</dispatcher>
  <dispatcher>FORWARD</dispatcher>
</filter-mapping>

3 configuration urlrewrite. xml

Add urlrewrite.xml to the WEB-INF directory of the project.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.1//EN"
    "http://tuckey.org/res/dtds/urlrewrite3.1.dtd">
<urlrewrite>
   <rule>
    <note>
      The rule means that requests to /test/status/ will be redirected to /rewrite-status
      the url will be rewritten.
    </note>
    <from>^/vweb/view/index\-([0-9]+)$</from>
    <to>/vweb/view/index.shtml?_vwebid=$1&type=show</to>
  </rule>
</urlrewrite>

Summary:

urlrewrite is a filter that will filter all requests from the user and redirect them if they meet the rules. The rule of from in rule node USES regular expression to match by default,

When the user accesses the server, URL will be compared with this configuration. If it conforms to the rules, it will jump according to the configuration in to node below. The default is forward jump.

Simple analysis 1 below the above regular expression "^/vweb/view/index-([0-9]+)" : "" matches the starting position of the input string" "matches the ending position of the input string

"\" escapes the character, meaning that the "-" does not have a special meaning, but is a simple string.
"([0-9] +)" :
"[0-9]" : a string of any data from 0 to 9
The "+" matches one or more of the characters that precede it.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: