Analysis of UrlRewrite Concept Principle and Usage

  • 2021-11-14 05:30:02
  • OfStack

URL Rewrite, or URL rewrite, is the process of redirecting incoming Web requests to other URL. The most common application of URL Rewrite is URL pseudo-static, which is a technology to display dynamic pages as static pages. For example, http://www.123.com/news/index.asp? id=123 After using UrlRewrite conversion, it can be displayed as http://www.123. com/news/123. html

What's the use of URL and Rewrite?

1, the first is to meet the requirements of perception.
For perfectionist web designers, even the addresses of web pages want to look as concise and lively as possible. Like http://www.123. com/news/index.asp? The webpage address of id=123 is naturally without aesthetic feeling, but with UrlRewrite technology, you can easily display it as http://www.123. com/news/123. html.

2. Secondly, the programming language used by the website can be hidden, and the portability of the website can be improved.
When every page of the website is marked with the development language. asp/. aspx/. php, others can see what language your website is made in with one eye. And when you change the language of the website, you need to change a lot of links. Moreover, when a page changes its extension, its pagerank disappears and starts from scratch. We can use UrlRewrite technology to hide our implementation details, so that it is very convenient to modify and transplant, and pagerank is not lost at all.

3. The last and most important role is to help search engines better crawl the content of your website.
In theory, search engines prefer static pages, and the search engine scores static pages 1 higher than dynamic pages. Therefore, UrlRewrite can make the pages of our website easier to be included by search engines.

For Java, refer to UrlRewriteFilter, address: http://tuckey.org/urlrewrite/.

Official Profile: A Java Web Filter for compliant web application such as Tomcat, JBoss to to rewrite URLs before a a a a a a a a a a a a a a a EN_rewrite!

1. Add Jar package urlrewritefilter-4. 0.3. jar to Lib

2. Add filter configuration in 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> 

For more information about configuration, click here!

3. Add urlrewrite. xml to your WEB-INF, click to see the example.

Here, for example, I wrote node configurations for two functions:


<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN" 
    "http://www.tuckey.org/res/dtds/urlrewrite4.0.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>/test/status/</from> 
    <to type="redirect">%{context-path}/index.jsp</to> 
  </rule> 
  <outbound-rule> 
    <note> 
      The outbound-rule specifies that when response.encodeURL is called  
      (if you are using JSTL c:url) 
      the url /rewrite-status will be rewritten to /test/status/. 
 
      The above rule and this outbound-rule means that end users should never see the 
      url /rewrite-status only /test/status/ both in thier location bar and in hyperlinks 
      in your pages. 
    </note> 
    <from>/rewrite-status</from> 
    <to>/test/status/</to> 
  </outbound-rule> 
</urlrewrite> 

The index. jsp page reads as follows:


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<html> 
 <body> 
   <c:url var="myURL" value="/rewrite-status" /> 
   <a href="${myURL }" rel="external nofollow" >AAAAA</a> 
 </body> 
</html> 

Note has made it very clear

The first function is conversion, when/test/status/is requested, index. jsp is actually requested

The second function is to display the conversion of URL on the page. Here, JSTL c: url must be used to convert the value part to the specified path to mask URL

4. Actual results

When/test/status/is requested, index. jsp is actually requested
index. jsp page actual output HTML content is:


<html> 
 <body> 
   <a href="/f/test/status/" rel="external nofollow" >AAAAA</a> 
 </body> 
</html> 

Related articles: