URL rewrite in springboot (urlwrite)

Source: Internet
Author: User

In the daily website access, the dynamic address will be converted to pseudo-static address.

For example: Access to the news column/col/1/, which is the original address, if such access, not conducive to search engine retrieval included, while security is not very good.

After the transformation:

/col/1.html.

Transformation method:

1. Add Urlrewritefilter

<Dependency>    <groupId>Org.tuckey</groupId>    <Artifactid>Urlrewritefilter</Artifactid>    <version>4.0.4</version></Dependency>

2. Configure Beans

Importjava.io.IOException;ImportJavax.servlet.FilterConfig;Importjavax.servlet.ServletException;ImportOrg.springframework.beans.factory.annotation.Value;Importorg.springframework.context.annotation.Configuration;ImportOrg.springframework.core.io.Resource;Importorg.tuckey.web.filters.urlrewrite.Conf;ImportOrg.tuckey.web.filters.urlrewrite.UrlRewriteFilter; @Configuration Public classUrlrewritefilterconfigextendsUrlrewritefilter {Private Static FinalString url_rewrite = "Classpath:/urlrewrite.xml"; //Inject the Resource from the given location@Value (url_rewrite)PrivateResource Resource; //Override The Loadurlrewriter method, and write your own implementation  protected voidLoadurlrewriter (Filterconfig filterconfig)throwsservletexception {Try {      //Create a Urlrewrite Conf object with the injected resourceConf Conf =NewConf (Filterconfig.getservletcontext (), Resource.getinputstream (), Resource.getfilename (),"@@[email protected]@");    checkconf (conf); } Catch(IOException ex) {Throw NewServletexception ("Unable to load URL rewrite configuration file from" +Url_rewrite, ex); }  }}

Reference URL: http://blog.jdriven.com/2016/02/urlrewritefilter-load-configuration-with-spring-resourceloader/

3. Configure Urlrewrite.xml

<?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>    <!--Column Home -    <Rule>        < from>^/col/(\w+) \.html$</ from>        < to>/col/$1/</ to>    </Rule>    <!--Column List page, note that there is no add $ after HTML, because there are several parameters -    <Rule>        < from>^/col/list/(\w+)/(\w+) \.html</ from>        < to>/col/list/$1/$2/</ to>    </Rule>    <!--Article details page -    <Rule>        < from>^/art/(\w+) \.html$</ from>        < to>/art/$1/</ to>    </Rule>        <!--static Web page -    <Rule>        < from>^/static/(\w+) \.html$</ from>        < to>/static/$1/</ to>    </Rule></Urlrewrite>

For configuration instructions, please refer to: http://blog.163.com/zhangmihuo_2007/blog/static/27011075201351433716225/

At this point the configuration is complete, start the test, pay attention to the red box, instructions loaded urlwrite.

Visit the page below and succeed!

URL rewrite in springboot (urlwrite)

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.