Automatically add subdomain "www" for my webapp

How can I set Tomcat to automatically redirect to "www"? I want if a user is a member of my domain, for example:

mydomain.com

he will be redirected to: www.mydomain.com

+6
java web-applications url-rewriting tomcat
source share
4 answers

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

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

This way you will see that everything that www does not use starts using

UPDATE As you already mentioned, you do not have Apache, I remembered that a year ago I used this one . This is almost the same as mod_rewrite, and is fully supported by Tomcat. I used it with resin, though, but I know that it works the same.

The most important thing about this is that it also works on the "mod_rewrite style", as you can see here . The only reason I did not continue to use it is because it will do this at the server level, as opposed to the web server level. It is understood that this will cause the JVM to interpret the redirect.

It works the same, but, as mentioned earlier, it can sue in the same way as you would use on Apache.

+5
source share

tuckey url rewrite filter can be used like this to properly redirect:

 <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</to> </rule> 
+6
source share

wwwizer.com offers a free naked domain redirect http://wwwizer.com/naked-domain-redirect

Call your bare domain, such as mysite.com, to your IP address and they will do 301 redirects to www.mysite.com

Using 301 redirects is the recommended way to optimize SEO.

+1
source share

Using Tuckey UrlRewriteFilter , you can also use the following rule:

 <rule> <condition type="header" name="host" operator="equal">^[^.]+[.][^.]+$</condition> <from>^/.*</from> <to qsappend="true" type="redirect" last="true">${replaceFirst:%{request-url}://://www.}</to> </rule> 

This is very interesting because nothing is hard-coded: it works for any domain and saves the port number and protocol.

Functions seem to have been implemented since v3.1.


There is a known issue with the qsappend attribute: https://code.google.com/p/urlrewritefilter/issues/detail?id=116 . You need to implement the fix specified in the link.

Just create the org.tuckey.web.filters.urlrewrite package in your webapp, create the NormalRule class in it and copy / paste the contents of the following class: http://urlrewritefilter.googlecode.com/svn/trunk/src/main/java/org /tuckey/web/filters/urlrewrite/NormalRule.java .

0
source share

All Articles