的URL标记为重复的内容。是的,如果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重定向的起源仍然被searchbots索引是非常重要的,但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.
}
对“www.domain.com/index.xhtml”的请求是一样的,因为'index.xhtml'可能在您的webapps文件夹中是公开的。 –
你是对的。但我只是想避免重复的内容。怎么做。你的意思是,现在我必须隐藏index.xhtml并编辑web.xml –