Assuming the old url was a .jsp, here is a JSP snippet to send a 301 redirect:
<%
String redirectURL = "http://.../";
response.setStatus(301);
response.setHeader( "Location", redirectURL);
response.setHeader( "Connection", "close" );
%>
If you are using Wicket, you can instruct the wicket filter in your web.xml file to ignore the said url, otherwise wicket filter would try to serve the JSP page as a Wicket page, which would not work.
<filter>For an arbitrary (non-jspP) url, you might actually need to create a wicket page redirecting from the old url to the new one. Here is the relevant code:
<filter-name>a name</filter-name>
<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
<init-param>
<param-name>ignorePaths</param-name>
<param-value>oldURL.jsp,other_urls_to_ignore,...</param-value>
</init-param>
</filter>
public class MyRedirectingPage extends WebPage {Within your
public MyRedirectingPage() {
final String newURL = "http://...";
RedirectRequestTarget target = new RedirectRequestTarget(redirectURL) {
@Override
public void respond(RequestCycle requestCycle) {
WebResponse response = (WebResponse) requestCycle.getResponse();
HttpServletResponse servletResponse = response.getHttpServletResponse();
servletResponse.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
servletResponse.setHeader("Location", newURL);
servletResponse.setHeader("Connection", "close");
}
};
getRequestCycle().setRequestTarget(target);
}
}
WebApplication
class, you also need to mount to the old url.mountBookmarkablePage("path/to/old/url", MyRedirectingPage.class);
1 comment:
This works great! Thanks for the post :)
Post a Comment