Tomcat Configuration 301 redirection (urlrewrite URL rewrite)

Source: Internet
Author: User

Tomcat by default the domain name without www will not jump to the domain name with www, and can not be configured as Apache. htaccess to achieve. If you want to redirect the domain name without "www" to the domain name with "www", and do not want to write code, you can use Urlrewritefilter to implement it.

1. Introduction

Urlrewritefilter is a web filter for overwriting URLs, similar to Apache's mod_rewrite. Applies to any Web application server (such as tomcat,jboss,jetty,resin,orion, etc.). Its typical application is to static dynamic URLs, so that search engine crawler crawl your dynamic Web pages.

2. Download

Download Urlrewritefilter

wget Http://urlrewritefilter.googlecode.com/files/urlrewritefilter-4.0.3.jar

And put it under Tomcat's web-inf/lib.

3. Configure Tomcat

Edit Web-inf/web.xml Join before other servlet mapping

<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>
4. Add a Jump Rule

Create a new Urlrewite.xml file under Web-inf and join the jump rule

<urlrewrite>  <rule>      <name>seo redirect</name>      <condition name= "host" Operator= "NotEqual" >^www.example.com</condition>      <condition name= "host" operator= "NotEqual" >^ Localhost</condition>      <from>^/(. *) </from>      <to type= "Permanent-redirect" last= "true" >http://www.example.com/$1</to>  </rule></urlrewrite>
Reference articles

http://nematodes.org/martin/2010/02/04/301-permanent-redirect-with-tomcat-howto/

http://tuckey.org/urlrewrite/

Tomcat Configuration 301 redirection (urlrewrite URL rewrite)

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.