July 24, 2019

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.

Resolution.

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>^(.*)lt;/from><to type=”redirect”>http://www.example.com$1</to></rule></urlrewrite>

Note: Replace example.com with original domain name.