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

infrancoAsked:
Who is Participating?
 
caterham_wwwConnect With a Mentor Commented:
for a permanental redirect, change
<to type="redirect"> into <to type="permanent-redirect">
0
 
caterham_wwwCommented:
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

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

Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.