Link to home
Start Free TrialLog in
Avatar of infranco
infranco

asked on

301 Redirects and Canonicalization in Tomcat 6

Tomcat 6 is acting as both our Application Server as well as our Web Server (that is, we're not using Apache/httpd).

In regular Apache, the global redirection of

http://sampledomain.com 
to
http://www.sampledomain.com 

can be done using the .htaccess file. Tomcat has no such file.

Does anyone have any ideas as to how non-WWW URLs should be 301 redirected to WWW URLs in Tomcat?

Thank you!

(note, in our implementation, Apache webserver is *not* running so mod_jk and mod_proxy_ajp are unavailable).



*in .htaccess*
 
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^sampledomain.com [nc]
rewriterule ^(.*)$ http://www.sampledomain.com/$1 [r=301,nc]

Open in new window

Avatar of caterham_www
caterham_www
Flag of Germany image

You might be looking for the UrlRewriteFilter http://tuckey.org/urlrewrite/

and the rule in a WEB-INF/urlrewrite.xml might look like
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite
        PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN"
        "http://tuckey.org/res/dtds/urlrewrite3.0.dtd">
 
<urlrewrite>
<rule>
<name>redirect</name>
<condition name="host" operator="equal">^sampledomain\.com</condition>
<from>^/(.*)</from>
<to type="redirect">www.sampledomain.com/$1</to>
</rule>
</urlrewrite>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of caterham_www
caterham_www
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of infranco
infranco

ASKER

UrlRewriteFilter looks really interesting. We'll try it out (along with caterham_www's code) and let everyone know how it works out.

Thanks!