How do I remove the www from my domain URL

I am using a tomcat server and I have installed the SSL by changing strings in server.xml and web.xml file.

Problem is, When I browse with domain.com, www.domain.com, in both the cases it is browsing. But I want it to redirect to domain.com only.

How would I do that?

Any help would be appreciated.

Thanks


Solution 1:

This can be achieved by Rewrite Valve, it's pretty similar to mod_rewrite in httpd :

1- Include rewrite valve in your global context file context.xml or your host context server.xml , I recommend it here :

<Host>
<Context> 
<Valve className="org.apache.catalina.valves.rewrite.RewriteValve" /> 
</Context>
</Host>

2- Create rewrite valve configuration file under the host configuration folder as the above :

$CATALINA_HOME/conf/Catalina/<host_name>/rewrite.config

3- Write your rewrite rule and save the configuration file :

RewriteRule ^(.*)$ http://domainname.com/$1 [L]

3- Restart tomcat

For a comprehensive review, check the official documentation