在<welcome-file> </welcome-file>上使用重定向而不是转发

时间:2013-11-24 16:49:05

标签: jsf redirect seo web.xml welcome-file

在我的web.xml文件中,我配置了:

<welcome-file-list>
    <welcome-file>index.xhtml</welcome-file>
</welcome-file-list>

这意味着,当我输入网址www.domain.com时,index.xhtml文件用于呈现。但是当我输入www.domain.com/index.xhtml时,结果是一样的。 它被称为重复内容? 对我的项目来说这不是问题,但对SEO来说是个大问题。 如何在键入网址www.domain.com/index.xhtml时重定向到www.domain.com页面,而不是让它执行转发?

2 个答案:

答案 0 :(得分:2)

如果同一域中有另一个网址返回完全相同的响应,则网址会被标记为重复内容。是的,如果SEO很重要,你一定要担心这个。

解决此问题的最简单方法是在index.xhtml的头部提供所谓的规范网址。这应该代表首选项的URL,在您的特定情况下,显然是文件名为

的URL
<link rel="canonical" href="http://www.domain.com/index.xhtml" />

这样http://www.domain.com将被编入索引为http://www.domain.com/index.xhtml。并且不再导致重复内容。但是,这并不会阻止最终用户能够收藏/共享不同的URL。

另一种方法是将HTTP 301重定向配置为首选项URL。非常重要的是要理解302重定向的来源仍然由搜索机器人索引,但301重定向的来源不是,只有目标页面被索引。如果您使用的是HttpServletResponse#sendRedirect()默认使用的302,那么您仍然会有重复的内容,因为这两个网址仍然被编入索引。

以下是此类过滤器的启动示例。只需将其映射到/index.xhtml,并在URI不等于所需路径时执行301重定向。

@WebFilter(urlPatterns = IndexFilter.PATH)
public class IndexFilter implements Filter {

    public static final String PATH = "/index.xhtml";

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        String uri = request.getContextPath() + PATH;

        if (!request.getRequestURI().equals(uri)) {
            response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); // 301
            response.setHeader("Location", uri);
            response.setHeader("Connection", "close");
        } else {
            chain.doFilter(req, res);
        }
    }

    // init() and destroy() can be NOOP.
}

答案 1 :(得分:0)

要删除重复内容,请设计包含网址格式/*的过滤器。如果用户位于根域,则重定向到index.xhtml网址。

@WebFilter(filterName = "IndexFilter", urlPatterns = {"/*"})
public class IndexFilter implements Filter {

    public void doFilter(ServletRequest req, ServletResponse resp,
        FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) resp;
    String requestURL = request.getRequestURI().toString();
    if (request.getServletPath().equals("/index.xhtml") &&
                !requestURL.contains("index.xhtml")) {
        response.sendRedirect("http://" + req.getServerName() + ":"
                + request.getServerPort() + request.getContextPath()
                +"/index.xhtml");
    } else {
        chain.doFilter(req, resp);
    }
}
 }