Jsf 在<;上使用重定向而不是转发;欢迎文件>;
在我的web.xml文件中,我配置了:Jsf 在<;上使用重定向而不是转发;欢迎文件>;,jsf,redirect,seo,web.xml,welcome-file,Jsf,Redirect,Seo,Web.xml,Welcome File,在我的web.xml文件中,我配置了: <welcome-file-list> <welcome-file>index.xhtml</welcome-file> </welcome-file-list> index.xhtml 这意味着,当我键入URLwww.domain.com,将使用index.xhtml文件进行渲染。但是当我输入www.domain.com/index.xhtml时,结果是一样的。 它被称为重复内容吗? 这对我的
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
index.xhtml
这意味着,当我键入URLwww.domain.com
,将使用index.xhtml
文件进行渲染。但是当我输入www.domain.com/index.xhtml
时,结果是一样的。
它被称为重复内容吗?
这对我的项目来说不是问题,但对搜索引擎优化来说是个大问题。
键入URL
www.domain.com
时,如何重定向到www.domain.com/index.xhtml
页面,而不是让它执行转发?要删除重复内容,请使用URL模式设计一个过滤器/*
。如果用户位于根域上,则重定向到index.xhtml
URL
@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);
}
}
}
当同一域上的另一个URL返回完全相同的响应时,URL被标记为重复内容。是的,如果SEO很重要的话,你肯定应该担心这一点 解决这个问题的最简单方法是在
index.xhtml
的头部提供一个所谓的规范URL。这应该表示首选的URL,在您的特定情况下,显然是带有文件名的URL:
<link rel="canonical" href="http://www.domain.com/index.xhtml" />
对
www.domain.com/index.xhtml
的请求是相同的,因为index.xhtml
可能位于您的webapps文件夹中,该文件夹是公用的。您的回答是对的。但我只是想避免重复的内容。如何做到这一点。你的意思是,现在我必须隐藏index.xhtml并编辑web.xmlsendRedirect()
做的是302,而不是301。filter的其余代码也不令人兴奋,虽然它完成了它的工作,但它显然是由初学者编写的,并且有几件事情可以做得更干净。谢谢@Masud,但我想问更多。这种现象被称为重复内容吗?我认为他不能以正确的方式讲述SEO,因为他已经没有在第一时间使用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.
}