How to redirect URLs with trailing slash to the corresponding ones without it?

前端 未结 7 1665
遥遥无期
遥遥无期 2020-12-05 10:12

Spring MVC (3.0) considers URLs with and without trailing slashes as the same URL.

For example:

http://www.example.org/data/something = http:

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 11:01

    I think you best option would be to do this before entering in Spring web's servlet, using UrlRewriteFilter. This will ensure that your redirect rules would not impact your controllers.

    Please note that you write the rules in your .war project, not in an apache with mod_rewrite.

    Go here for the library's project on googlecode.

    in the urlrewrite.xml write :

    
    
    
          
          Remove trailing slash
          ^(.*)/$
          $1
          
    
    

    In the web.xml of your application, add :

    
        UrlRewriteFilter
        org.tuckey.web.filters.urlrewrite.UrlRewriteFilter
           
                confPath
                /WEB-INF/urlrewrite.xml
            
    
    
        UrlRewriteFilter
        /*
        REQUEST
    
    

    Beware, the declaration order of the filters in the web.xml is important, so try to declare this one before anything from spring.

    Of course, this is but a fraction of what UrlRewriteFilter can do.

    Regards.

提交回复
热议问题