How to set redirection for without WWW to with WWW URL in Tomcat using UrlRewriteFilter

 

What is the meaning of WWW?

WWW stands for World Wide Web is an information space with the combination of all resources and users on the Internet.  Which allows the retrieval and display of text and media to your computer. It is a basically a system of Internet web servers where the documents are formatted into an HTML language which supports links to other documents as well as graphics audio and videos.

Difference between WWW vs non-WWW?

When adding www in front of the domain or website it acts has a hostname. Which helps with DNS flexibility. Which restrict cookies when using multiple subdomains. The reasons to use www primarily apply to the largest websites which receive millions of page views per day, websites with a large number of services across several subdomains, and virtually any website hosted in “the vps or cloud server” by an application service provider.   

Whereas non-WWW domains also referred has naked domains do not have a technical advantage.

What is the use of www.?

Using www makes you more prepared to handle the challenges of a growing website beyond a single server which is good for SEO to improve the page ranking.

Web Hosting
Web Hosting
at a very Affordable Price
Starts at Rs. 69
VPS Hosting
VPS Hosting
Customized for all technologies
Starts at Rs. 732
Cloud Hosting
Cloud Hosting
Security & Performance Guaranteed
Starts at Rs. 2680
Windows Hosting
Windows Hosting
Reliable Windows Web Hosting
Starts at Rs. 199
Dedicated Hosting
Dedicated Server
Fully managed Servers in India
Starts at Rs. 4499
Linux Hosting
Linux Hosting
Reliable Linux web hosting plans
Starts at Rs. 69

What is UrlRewriteFilter?

UrlRewriteFilter is a java web Filter for any compliant web application servers like Tomcat, JBoss, resin running in HostingRaja VPS Hosting Server, which enables rewrite URLs before they get to your code. It is just like an Apache's mod_rewrite.
Resolution.

1) Download UrlRewriteFilter jar file from http://hostingraja.info/urlrewritefilter-4.0.3.jar and upload this file into WEB-INF/lib directory of your website.
Example your website folder is /home/hostingraja/public_html, kindly upload this file to /home/hostingraja/public_html/WEB-INF/lib this directory

Or else add the maven dependency in your pom.xml file under your website folder

<dependency>
<groupId>org.tuckey</groupId>
<artifactId>urlrewritefilter</artifactId>
<version>4.0.3</version>
</dependency>

2) Go to WEB-INF/web.xml in your website folder 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>

Refer the below screenshot




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.

The above code snippet helpful to redirect the domain from without www to with www . if you want to some other rule then write a proper rule on this file too.

4) Restart Tomcat service to reflect the changes. Go to your tomcat folder and run the below command for restart the tomcat


Example (/usr/local/tomcat8 or /usr/local/tomcat9 like that )


sh bin/shutdown.sh

sh bin/startup.sh