views:

96

answers:

2

How can I set Tomcat to automatically redirect to "www"? I want that if a user enters my domain like:

mydomain.com

he will be redirected to: www.mydomain.com

+1  A: 

If you are using Apache, simple do (on htaccess):

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

This way you make sure anything that is not using www starts using

UPDATE As you mentioned you don't have apache, I remembered I used this about a year ago. It does pretty much the same as mod_rewrite, and is fully supported by Tomcat. I used it with resin though, but I know it works the same way.

Greatest thing about it, is that it also runs on "mod_rewrite style", as you can see here. The only reason why I didn't continue using it, is because it will end up doing it at a server level, as opposed to a webserver level. Meaning it will call the JVM to interpret the redirect.

It works the same way though, and as mentioned before, can sue exactly the same thing you'd use on Apache.

Marcos Placona
Hi,Thanks for your answer.I know how it is done with Apache. Unfortunately, I don't have Apache, only Tomcat.
bashan
@bashan I've updated my answer with some more information
Marcos Placona
A: 

The tuckey url rewrite filter can be used like this to do the proper redirection:

<rule>
  <name>Canonical Hostnames</name>
  <condition name="host" operator="notequal">^www.mydomain.com</condition>
  <condition name="host" operator="notequal">^$</condition>
  <from>^/(.*)</from>
  <to type="redirect" last="true">http://www.mydomain.com/$1&lt;/to&gt;
</rule>
bashan