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

前端 未结 7 1648
遥遥无期
遥遥无期 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
    2020-12-05 10:48

    Based on SEO, I think it is important to make a distinction.

    If the URL that finished in the trailing slash exist, is indexed in the search engines and there are links on Internet, a permanent redirection (301) is required as Uddhav Kambli says. The standard redirection (302) will be better than having a duplicated URL, but is not good enough.

    However, if the URL never existed, it is not indexed on Internet and there are no external links, the URL does not exist. Therefore a 404, page not found, is a better fit.

    WEB-INF/urlrewrite.xml

    
    
    
        
            Remove trailing slash
            ^(.+)/$
            404
            null
        
    
    

    And in order to complete the configuration ...

    add to WEB-INF/web.xml

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

    Maven

    
        org.tuckey
        urlrewritefilter
        4.0.3
    
    

提交回复
热议问题