Updating WordPress Post Urls by Category with Spring Boot

We blogged about our NixMash Blog application (on GitHub.) In the process of transitioning from WordPress we tossed the /{category}/{postName} format of Post Permalinks and are instead going with a straight-up /post/{postName} Url.

There are a ton of ways to approach this and for good or bad, this post describes how we're doing it with Spring Boot. We created about 20 categories on the WordPress blog and we're going to send a PERMANENT_REDIRECTION response for all of them. However, we are only supporting automatic redirection for the most active WordPress categories like Java and Linux. For categories like Ruby on Rails or PHP, we're moving on.

Here's the strategy in pictures starting with a post permalink with the new /post/{postName} format.

A supported WordPress category. A 301 Response and auto redirection to the post.

A category that no longer represents the blog's focus like Ruby on Rails. The user can still get to the post, but they'll have to read a blurb about the new site first.

The Tests

We'll show the H2 Integration Tests first to reinforce the behavior we're going for.

  • Good Categories -> 301 and auto-redirection to /post/{postName}
  • Non-existent Categories -> 404
  • Old, Unsupported Categories -> 301 and throw a CategoryNotSupported Exception

The Controller

Here are the Controller methods playing the traffic cop. Spring MVC @RequestMappings do not support SpEL, so we're doing the next best thing with string concatenation. Please note that the eclipses are for readability only.

CategoryNotSupported Exception

We didn't need to throw an exception for non-supported Categories, but it seemed appropriate. Here's the exception's handler in the @ControllerAdvice GlobalController class.

Source Code Notes for this Post

Source code discussed in this post can be found in my NixMash Blog project located on GitHub.

Posted February 24, 2017 02:45 PM EST

More Like This Post