Regexp and redirections

Created: 2013/03/26 22:31:04+0000

Changing the URIs meant I was supporting two types of URIs for the dynamic content. Then I had a better idea. I could use the UrlRewriteFilter and regular expressions to match the old style URI and redirect to the new.

I enabled matching on the query string and wrote a regular expression to pick out the IDs. The UrlRewriteFilter does not match over the query string by default, it needs to be enabled. The UrlRewriteFilter uses the regular expression engine in the Java standard library. For the latest version of the article the regular expression was simple.

^/article\?articleId=(\d+)

Starts of with the beginning of the line. Matches the URL, article and then goes onto the query string. The query string delimiter needs to be escaped and as an XML file the backslash does not need to be escaped as it would be in a Java string literal. After the article ID is the first group. The first group greedily matches 1 or more digits.

If the URI matches the regular expression, the filter performs a 301 redirect using back references to the new style URI. Now I only support one style of URI.