Wicket: A Neat Url Encoding Strategy and some basic SEO

December 9th, 2008 by  |  Published in SEO, Wicket  |  1 Comment

After a year of working extensively with Apache Wicket I want to share some stuff I’ve been using over and over again and might be useful for you as well. In this first post it’s a custom URL Encoding Strategy and some related basic SEO.

Pages often depend on entities – e.g. a Customer or a product and as those pages should be bookmarkable as well, there has to be a unique way to tell the page which entity to use. As entities either don’t have unique names or one wants to avoid having an (database) index over the name column, the index-ID is for most applications the way to go. As it’s usually undesireable to expose the database ID of an entity in the URL, it has to be encoded and decoded somehow to get from myshop.com/customer/12 to myshop.com/customer/ea34cff … or something like that.

Using a custom Url-mounting strategy makes it a breeze:

public abstract class IndexedIdEncryptingUrlCodingStrategy extends LowercaseOnlyIndexedUrlEncodingStrategy {
 
private final int[] _encryptedParams;
// serverroot is e.g. http://www.mypage.com - required for 301 redirects in baseclass
public IndexedIdEncryptingUrlCodingStrategy(final String mountPath, final String serverRoot, final Class bookmarkablePageClass, final int... encryptedParmas) {
super(mountPath, serverRoot, bookmarkablePageClass);
_encryptedParams = encryptedParmas;
}
 
public IndexedIdEncryptingUrlCodingStrategy(final String mountPath, final String serverRoot, final Class bookmarkablePageClass, final String pageMapName, final int... encryptedParmas) {
super(mountPath, serverRoot, bookmarkablePageClass);
_encryptedParams = encryptedParmas;
}
 
@SuppressWarnings("unchecked")
@Override
protected void appendParameters(final AppendingStringBuffer url, final Map params) {
for (final int index : _encryptedParams) {
final String indexStr = Integer.toString(index);
final Object val = params.get(indexStr);
if (val != null && val instanceof Integer) {
params.put(indexStr, encodeId((Integer) val));
}
}
super.appendParameters(url, params);
}
 
@Override
protected ValueMap onDecodeParameters(final String urlFragment, final ValueMap urlParameters) {
 
for (final int index : _encryptedParams) {
final String indexStr = Integer.toString(index);
final Object val = urlParameters.get(indexStr);
if (val != null && val instanceof String) {
urlParameters.put(indexStr, decodeId((String) val));
}
}
 
return urlParameters;
}
 
protected int[] getEncryptedParams() {
return _encryptedParams;
}
 
protected abstract String encodeId(Integer val);
 
protected abstract Integer decodeId(String val);
 
}

This class’s baseclass is used for SEO only: it just checks if there are only lowercase letters in the URL and does a 301 redirect to the lowercase variant of the page if not.
Why lowercase only: The first of the Ten SEO Commandments (if there was such a thing) is: “Thou shalt only have one URL per page” – and for bots http://www.myshop.com/customer/Foobar and http://www.myshop.com/customer/fOoBar are two different URLs – which is a bad thing as far as duplicate content is concerned. Well, it could also be uppercase only or camelcase only – but lowercase makes it easier to read for humans and is also easier to implement than camelcase.

Then just mount the page like that:

mount(new IndexedIdEncryptingUrlCodingStrategy("/customer", "http://www.mypage.com", HomePage.class, "id"));

and construct the page params like that:

final PageParameters params = new PageParameters();
params.add("id", myEntity.getId());
 
// add e.g. a link
add(new BookmarkablePageLink("link", HomePage.class, params));

Download the files:
IndexedIdEncryptingUrlCodingStrategy.java
LowercaseOnlyIndexedUrlEncodingStrategy.java

In my next post I’ll get a bit more into Wicket & SEO: Show a more SEO way to encode URLs, how to deal with the JSESSIONID and how to apply the code shown today on a Page. Stay tuned – and consider subscribing

PS: If “thou convet my code”: the code is public domain, feel free to do whatever you like to do with it. If you have any suggestions or implement enhancements, just post them in the comments.

Responses

  1. Daan says:

    December 10th, 2008 at 7:51 pm (#)

    Hi, great article!Some related articles on Wicket Rest can be found here:Wicket Extreme Consistent URL’sCreate RESTful URLs with Wicket

Leave a Response