的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 –