Forcing tomcat to use www.domain.com instead of domain.com
apache, tomcat
Solution
Tomcat doesn't do anything with .htaccess files: that's an Apache httpd thing.
You have a couple of options, here:
- Writer a Filter that checks the hostname used to access your webapp and redirects if it doesn't have the requisite "www." prefix.
- Put two <Host>s in your conf/server.xml file: one with hostname "blah.com" and one with hostname "www.blah.com" where "blah.com" is the default. Then, deploy your webapp only into "www.blah.com" and deploy a simple webapp into "blah.com" that redirects everything to the desired hostname. You can use http://www.tuckey.org/urlrewrite/ to do the real work for you if that helps.
Problem
At the moment I'm using Apache's mod_rewrite to redirect all html requests to http(s)://www.domain.com/blah instead of http(s)://domain.com/blah. Everything works fine except when I connect to tomcat at domain.com:8080. All I've done is naively put the same .htaccess file into my /opt/tomcat-7/webapps/ROOT directory, but it doesn't seem to work. I'm actually hoping I'm way off track with what I've tried above and there is a way I can always rewrite domain.com:8080 to www.domain.com:8080 without having to place a new .htaccess file into every webapp directory, but if not, what am I doing wrong in the above? I've searched all over Google for others having just this problem with tomcat to no avail. For the record, my .htaccess file is: ``` RewriteEngine on RewriteCond %{HTTP_HOST} !^$ RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteCond %{HTTPS}s ^on(s)| RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] ``` Cheers, Ben.