Reputation: 466
In my web.xml file, I configured:
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
It means, when I type a URL www.domain.com
, index.xhtml
file is used to render. But when I type www.domain.com/index.xhtml
, the result is the same.
is it called duplicated content?
This is no problem for my project but a big problem for SEO.
How can I redirect to www.domain.com/index.xhtml
page when typing URL www.domain.com
instead of letting it perform a forward?
Upvotes: 2
Views: 4334
Reputation: 1109302
An URL is marked duplicate content when there's another URL on the same domain which returns exactly the same response. And yes, you should definitely worry about this if SEO is important.
Easiest way to fix this is to provide a so-called canonical URL in the head of index.xhtml
. This should represent the URL of preference, which is in your particular case apparently the one with the filename:
<link rel="canonical" href="http://www.domain.com/index.xhtml" />
This way the http://www.domain.com
will be indexed as http://www.domain.com/index.xhtml
. and not cause duplicate content anymore. However, this will not stop endusers being able to bookmark/share different URLs anyway.
Another way is to configure a HTTP 301 redirect to the URL of preference. It's very important to understand that the origin of a 302 redirect is still indexed by searchbots, but the origin of a 301 redirect not, only the target page is indexed. If you would be using a 302 as by default used by HttpServletResponse#sendRedirect()
, then you would still end up having duplicate content because the both URLs are still indexed.
Here's a kickoff example of such a filter. Just map it on /index.xhtml
and perform a 301 redirect when the URI doesn't equal the desired path.
@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.
}
Upvotes: 2
Reputation: 21971
To remove duplicate content, design a Filter with URL patter /*
. If user on root domain than redirect to 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);
}
}
}
Upvotes: 0