Before I understood that Google cared little for JSessionID, I allowed URL rewriting (the default Tomcat behavior) in my Facelets application. For those that don't know already, Google does not like this with regards to SEO (the session ID on your URL) and I since have included the following in web.xml to rectify this:
<session-config>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
The problem is that I have many 500
errors from outdated links that are hurting my SEO because of sites referencing outdated links with these jsession ID's e.g,:
http://thejarbar.org/views/tutorials/linux/Netbeans-
Install.xhtml;jsessionid=8D0EF52E48E8BB8BF8A4D7E33F831EFF
I have fixed several errors with pages names with html or xhtml suffixes easily, by creating pages that redirect to updated links, but the problem I have is that I can't create a file name with a session id suffix as this won't be served by Tomcat.
Redirecting a dead HTML link is as simple as creating a plain (X)HTML page in the relevant directory and including a meta tag as follows (example):
- Dead link points to
.../Maven-Setup.xhtml
- Create a replacement
Maven-Setup.xhtml
in referenced location - In the replacement I simply add
<meta http-equiv="REFRESH" content="0;url=http://thejarbar.org/views/tutorials/linux/Maven- Setup-Tutorial-for-Linux-with-the-Jar-Bar-and-Yucca-Nel.xhtml"></meta>
How can I redirect request based on outdated urls with session ID's? I would obviously favor keeping these links as they may (or may not) be bringing traffic to my site and hence why I want to redirect.
Update I have created a redirect.jsp with the intention of mapping a url-pattern in web.xml to forward all incomming jsessionid requests to my new 'no jsession' site:
<servlet>
<servlet-name>redirect</servlet-name>
<jsp-file>/redirect.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>redirect</servlet-name>
<url-pattern>/*jsessionid*</url-pattern>
</servlet-mapping>
This is not forwarding the requests with jsession id and I have tested redirect.jsp
which forwards correctly.
Update Following Balusc's recommendation and another Filter I found here, I came up with this which does not seem to be working with regards to redirecting to my new site:
package org.thejarbar.web.filters;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import javax.servlet.*;
import javax.servlet.http.*;
public final class JSessionFilter implements Filter {
public void init(FilterConfig filterConfigObj) {
}
public void doFilter(ServletRequest _req, ServletResponse _res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) _req;
HttpServletResponse res = (HttpServletResponse) _res;
String url =req.getRequestURL().toString();
if(Pattern.compile(Pattern.quote(url), Pattern.CASE_INSENSITIVE).matcher("jsessionid").find()){
String redirectURL = "http://thejarbar.org";
res.setStatus(301);
res.setHeader("Location",redirectURL);
res.setHeader( "Connection", "close" );
res.sendRedirect(redirectURL);
}
chain.doFilter(req, res);
}
public void destroy() { }
}