I have URLRewirteFilter which checks if requested domain starts with www. and redirects to no-www url. How can I stop futher processing (invoking JSF app, call servlets etc.) when request is to be redirected? So far I have this:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; String sn = req.getServerName().toLowerCase(); if (sn.startsWith("www.")) { String url = "http://" + getDefaultDomain() + req.getContextPath() + req.getRequestURI(); HttpServletResponse resp = (HttpServletResponse) response; resp.reset(); resp.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); resp.setHeader("Location", url); } chain.doFilter(request, response); }
Just either add a return
statement to the end of the if
if (sn.startsWith("www.")) { String url = "http://" + getDefaultDomain() + req.getContextPath() + req.getRequestURI(); HttpServletResponse resp = (HttpServletResponse) response; resp.reset(); resp.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); resp.setHeader("Location", url); return; } chain.doFilter(request, response);
or add an else
if (sn.startsWith("www.")) { String url = "http://" + getDefaultDomain() + req.getContextPath() + req.getRequestURI(); HttpServletResponse resp = (HttpServletResponse) response; resp.reset(); resp.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); resp.setHeader("Location", url); } else { chain.doFilter(request, response); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With