开发者

java - Spring MVC: Redirect requests with old URLs to new URLs

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 U开发者_如何学GoRLs 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?


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜