如何将不带www的根域名301重定向到带www的主域名
@Override
public void init(FilterConfig filterConfig) throws ServletException {}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp= (HttpServletResponse)response;
String tempPathString = req.getRequestURI();
if(req.getRequestURL().indexOf("http://example.com")>=0){ // 不带www,跳转的首页 String queryString = (req.getQueryString() == null ? "" : "?"+req.getQueryString());
resp.setStatus(301);
resp.setHeader( "Location", "http://www.example.com"+tempPathString+queryString);
resp.setHeader( "Connection", "close" );
}
chain.doFilter(request, response);
}
@Override
public void destroy() {}