WWW to non-WWW Urls in Java Web Apps

There are a lot of guides out there on how to perform 301 Redirects from WWW to non-WWW urls (and vice versa) for straight HTML and PHP sites. We Java App developers don’t have the luxury of putting a Rewrite Rule in a site .htaccess file. Something like

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.nixmashuplinks\.com
RewriteRule (.*) http://nixmashuplinks.com/$1 [R=301,L]

If your Java site runs in Tomcat and Apache, here’s how to produce the same result.

For our example we’ll use my new site url of http://nixmashuplinks.com. To auto-redirect a www.nixmashuplinks.com url to nixmashuplinks.com we first add a www.nixmashuplinks.com site in Apache. The www site will serve as a placemarker and perform the redirection. Here is the Apache www.nixmashuplinks.com.conf file.

We don’t need to make any changes to the nixmashuplinks.com.conf file other than removing www.nixmashuplinks.com as a ServerAlias.

Add the www site in Apache, reload Apache, and you are good to go!