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.
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.
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
2) Go to WEB-INF/web.xml in your website folder and add below code (near the top above any Servlet mappings):
Refer the below screenshot
3) Create file urlrewrite.xml in WEB-INF and add below code:
<name>Domain Name Check for example.com</name>
<condition name=”host” operator=”equal”>^example.com</condition>
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 )