views:

948

answers:

7
+2  Q: 

Java and SEO URLS

I'm building a webapp using spring MVC and am curious as to whether there is any clean way to make SEO urls.

For example, instead of http://mysite.com/articles/articleId and the such, have:

http://mysite.com/articles/my-article-subject

+3  A: 

This might be of interest to you:

http://tuckey.org/urlrewrite/

If you are familiar with mod_rewrite on Apache servers, this is a similar concept.

Phill Sacre
Very handy, thank you.
hamo
This looks very useful - thanks
matt b
Nice! Very useful!!!
razenha
+1  A: 

http://mysite.com/articles/my-article-subject is a much stronger URL than http://mysite.com/articles/articleId - especially if the title and header tags match "my-article-subject" too and you have "my", "article" and "subject" in the content of the page.

Sohnee
A: 

If you're only looking for a SEO optimization you could design your URLs this way:

http://mysite.com/articles/my-article-subject/articleId

or

http://mysite.com/articles/articleId/my-article-subject

and just ignore the part my-article-subject when evaluating the urls.

Amazon does something like that with their URLs:

http://www.amazon.com/Dark-Crystal-Jean-Pierre-Amiel/dp/B00000JPH6/ref=sr_1_1?ie=UTF8&s=dvd&qid=1240561659&sr=8-1

Here the Text "Dark-Crystal-Jean-Pierre-Amiel" is totally irrelevant because the article is identified by the id B00000JPH6.

Edit: In fact I just noticed that right here on SO this exact technique is used to generate SEO-friendly URLs...

Benedikt Eger
Of course! That seems so obvious now that you pointed it out. The application can ignore it, but all the links have the subject content in it.
hamo
it's not overly user friendly though. You can't expect people to type that in.
John Polling
Users who want to type in the url can just type in the short version: http://mysite.com/articles/articleId
Mr. Shiny and New
A: 

For example if you want the url

http:///blog/11/12/2009/my-hello-world-post/

then configure the servlet mapping

<servlet>
<servlet-class>com.blog.Blog</servlet-class>
<servlet-name>blog</servlet-name>
<servlet-class>com.blog.Blog</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>blog</servlet-name>
<url-pattern>/blog/*</url-pattern>
</servlet-mapping>

and in the servlet code

String url = request.getPathInfo();
StringTokenizer tokens = new StringTokenizer(url,"/");
while(tokens.hasMoreTokens()){
out.println("
"+tokens.nextToken());
}

Use these params to get the data from database and display to user

Thej
A: 

The standard Java web frameworks are not ready for those kind of URL.

AFAIK, SpringMVC does not support this kind of URL.

There are two frameworks I'm sure that support this kind of URL: Mentawai and VRaptor.

razenha
A: 

You will probably need a combination of urlrewrite and I would suggesting looking at the ruby on rails plugin called friendly id (Im not allowed to add link, but easily googled, the author is norman on github) .
The issue becomes, if you don't want to include some sort of numeric unique identifier like /article/numberid/name-of-article ... you need to generate a slug for each article and make that some sort of unique text id.. and also handle duplicates and restricted words, if its user generated content.
Also if you switch to Spring 3.0 M3, you will not need to use url rewrite anymore.

hank hill
+1  A: 

If you're using the new Spring-MVC annotations, you can use the @RequestMapping and @PathVariable annotations:

@RequestMapping("/articles/{subject}")
public ModelAndView findArticleBySubject(@PathVariable("subject") String subject)
{
   // strip out the '-' character from the subject
   // then the usual logic returning a ModelAndView or similar
}

I think it is still necessary to strip out the - character.

Dick Chesterwood