301 Redirects Made Easy In Java

I’ve just released a new version of visural-common – 0.3.2.

This version includes a new Servlet Filter, which makes 301-redirects in Java a breeze.

You’d recall that 301 redirects are useful for SEO (and just general good practice) for when you move content to another place, or to create a “canonical” version of a piece of content. Matt Cutt‘s has done a few posts about 301 vs. 302 redirects and when you should use each, but this post pretty much summarizes the use-case for this filter.

Using the filter is easy, just create a class that extends from PermanentRedirectFilter:

public class MyRedirect extends PermanentRedirectFilter {
    public void configureRoutes() {
        // ... your config goes in here ...
        // e.g.
        fromHost("mydomain.com").to("www.mydomain.com");

        // urls work too
        fromURL("http://www.mydomain.com/index.jsp")
             .to("http://www.mydomain.com/");

        fromURL("http://www.mydomain.com/blog/post.jsp?p=123")
            .to("http://www.mydomain.com/blog/2010/06/how-to-do-301-redirects-in-java");

        // you could also use this to move your domain to another place altogether.
        // The same page url and parameters will be used after the host.
        fromHost("www.myolddomain.com").to("www.other.com");
    }
}

Pretty easy and efficient syntax.

You’ll then want to add this to your web.xml in the usual way for configuring a filter, e.g.:

    <filter>
        <filter-name>MyRedirectFilter</filter-name>
        <filter-class>com.mycom.MyRedirectFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>MyRedirectFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

Done and done – that’s all there is to it.

Related posts:

  1. ResourceTransformFilter – DataUris, LessCSS, Javascript and CSS Compression Made Easy!
  2. Detecting Which Browser In Java Servlet/Filter
  3. HTML Sanitizer added to visural-common…
  4. AOP, Annotation-based caching solution for Guice projects…
  5. 5 Minute Guide to Clustering – Java Web Apps in Tomcat

This entry was posted in Java, Software Engineering and tagged Google, Java, open-source, seo, visural-common, web. Bookmark the permalink.

4 Responses to 301 Redirects Made Easy In Java

  1. Jens says:

    When would be a good example of using this as opposed to just writing a simple redirect statement using apache?

  2. Not everyone runs vanilla apache in front of their Tomcat (or other container) instance.

    It’s also something that can change with your codebase and code release cycle rather than being an external piece of configuration.

    Just another tool in the toolkit :)

  3. Jens says:

    good point. thanks.

  4. Kevin Yang says:

    Hi Richard, thank you for sharing this. In my case, I use this filter to solve the ssl certificate error because I only have one certificate with mydomian.com but sometimes my clients use http://www.mydomain.com to access the application which causes a ssl certificate error.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>