Remove index.php from wordpress permalink on J2EE server, clean up URL for a J2EE app.

WordPress permalink URLs may contain index.php. The standard way to remove them on an apache server is to use a .htaccess file. However, if you are one of those few people who have wordpress on apache tomcat (application server) and see no advantage in installing a separate web server, there is a way in which ‘index.php’ can be removed from links using a pure J2EE approach.

The tutorial below is generic enough and can be used to clean up a url for any J2EE application. so if you have a link that reads as www.studytrails.com/java/spring.jsp and you want the user to be aware of only www.studytrails.com/java/spring/ then this is the tutorial for you

The concept : We will be using a servlet filter to filter requests. The filter will modify and forward links in a way that is seamless to the user.

Steps:

1. In the web.xml file , add the following lines (note that there is no web.xml in a wordpress application by default, you will have to create a new one and put it in WEB-INF directory of the wordpress application)

<filter-mapping>
<filter-name>BlogFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>BlogFilter</filter-name>
<filter-class>com.studytrails.BlogRedirectingFilter</filter-class>
</filter>

We pass all requests to a filter called BlogRedirectingFilter.

2. Create the Filter. Here’s the filter class

public class BlogRedirectingFilter implements Filter {

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		String requestUrl = request.getServletContext().getContextPath();
		String url = ((HttpServletRequest) request).getRequestURL().toString();
		if (url.endsWith("css") || url.endsWith("js") || url.endsWith("php")
				|| url.contains("index.php") || url.endsWith("png")
				|| url.endsWith("gif") || url.endsWith("ico")
				|| url.endsWith("jsp") || url.endsWith("jpg")
				|| url.endsWith("html") || url.endsWith("blog")
				|| url.endsWith("blog/")|| url.endsWith("wp-admin/")) {
			System.out.println(url);
		} else {
			// break and reconstruct URL
			String a = url.split("//")[1];
			// the context
			String context = a.split("/")[1];
			if ("blog".equals(context)) {
				String[] pathElements = a.split("/");
				// reconstruct URL
				String newUrl = "/index.php";
				for (int i = 2; i < pathElements.length; i++) {
					newUrl += "/" + pathElements[i];
				}
				newUrl += "/";
				System.out.println(newUrl);
				request.getServletContext().getRequestDispatcher(newUrl)
						.forward(request, response);
				return;
			}

		}
		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {

	}

}

quite simple actually, you can play around with the code to accomplish whatever you want. i.e. extract data from the request and pass it as parameter, remove parts of a link or beautify a link. Note that since the filter forwards the request, the user does not see the forwarded URL (use a requestdispatcher redirect, if you want user to see the actual URL, but then there is no need for this tutorial…)

Leave a Comment