How to set redirection for non www to www URL in tomcat using UrlRewriteFilter

Started by Manish, May 10, 2016, 06:41:37 PM

Previous topic - Next topic

Manish

Hi All,

You can set redirection for non-www URL to www URL in tomcat using UrlRewriteFilter. Below are the steps to set redirection:

1) Download UrlRewriteFilter to WEB-INF/lib directory of your website.

2) Go to WEB-INF/web.xml and add below code (near the top above any servlet mappings):



<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>




3) Create file urlrewrite.xml in WEB-INF and add below code:



<urlrewrite>
<rule>
              <name>Domain Name Check for example.com</name>
                <condition name="host" operator="equal">^example.com</condition>
               <from>^(.*)$</from>
               <to type="redirect">http://www.example.com$1</to>
      </rule>
</urlrewrite>




Note: Replace example.com with original domain name.

4) Restart tomcat service on the server.

That's all. Your website should now redirect from non www URL to www URL.