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 name registration india 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 they provide cheap vps hosting India solution which enables rewrite URLs before they get to your code. It is just like an Apache's mod_rewrite.
Resolution.
Follow the below steps :
Step 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
You can also 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>
Step 2: After you add jar or maven dependency >> 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
Step 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 the 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.
Step 4: Restart Tomcat service to reflect the changes. Go to your tomcat folder and run the below command for restarting the tomcat
Example (/usr/local/tomcat8 or /usr/local/tomcat9 like that )
sh bin/shutdown.sh
sh bin/startup.sh
Following the above steps you can set redirection for URL without WWW to with WWW
If you need any assistance , feel free to contact our technical team members as they are available 24/7 via phone call, email, ticket system or call out toll-free no 1800-123-8642.