Question

I have changed URL style of one of my websites to RESTful:

**Old URLs**                             **New URLs**
/article/all.ab                          /articles
/article/read.ab?id=345                  /articles/345/title-of-my-article

Until the search engines re-index my website with new URLs, people will continue to see old URLs on search engines. If they come to my site with an old URL, they will get a 404.

I want to create a mechanism (by writing a controller, error handler or whatever) that would:

  1. catch a request whose mapping was not found
  2. check if there is a new URL mapped for this request path.
  3. If new URL was found, redirect to that URL.
  4. Else show "404 Page not found" page.

How can this be done?

Was it helpful?

Solution

You could use a rewrite utility like this URL Rewrite Filter to rewrite your URLs before they are processed by your dispatcher servlet. We are using this approach a lot for user-friendly URLs in our web applications and this filter helps a lot. Its functionality is a lot like Apache's mod_rewrite.

Of course, it would require you to adjust your web application and redeploy it. But the ruleset is very easy using regex matches on URLs and would also allow to send redirects to the client (if desired).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top