SEO schema

Simple SEO friendly titles in Java

Sometimes, for SEO purposes, we need to put a string into a URL, like an article title for example.

Let’s say we have an article with the title The title of a nice & simple post !, what we want is the following valid URL:


  1. Set up the project
  2. Make a title URL friendly
  3. Write tests
  4. Conclusion

Set up the project

We will use:

Here is our Maven pom.xml file:

<project xmlns="" xmlns:xsi=""



        <!-- Guava -->

        <!-- JUnit -->

        <!-- AssertJ -->


Make a title URL friendly

We create the SeoString interface:

public interface SeoString {

    String toUrlFriendlyString();

Here we will implement a solution only for the English language, other languages would obviously need adjustements.

What we need is to:

  • remove every special characters except &
  • remove any trailing spaces
  • replace spaces with -
  • replace & with and

We create the EnglishTitle class:


import java.util.Objects;

public class EnglishTitle implements SeoString {

    private final String title;

    public EnglishTitle(String title) {
        this.title = Objects.requireNonNull(title);

     * Builds a url friendly string out of an original string, by :
     * - removing all non alphanumerical characters except '&'
     * - removing trailing spaces
     * - replacing whitespaces with a dash
     * - replacing & with 'and'
    public String toUrlFriendlyString() {
        String urlFriendly = title.toLowerCase()
                        .replaceAll("[^a-zA-Z0-9 &]", "")
                        .replace(' ', '-')
                        .replace("&", "and");
                return UrlEscapers.urlPathSegmentEscaper().escape(urlFriendly);

At the end we use Guava’s UrlEscapers.urlPathSegmentEscaper() to make sure our string will be correctly encoded for the final URL. (In reality, it is aldready the case, because we removed any special characters, but it is a good practice to ensure it is always the case).

See What every web developer must know about URL encoding for further informations.

Write tests

Let’s write a few tests to validate the behaviour of EnglishTitle.toUrlFriendlyString().

We create EnglishTitleTest in src/test/java:

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class EnglishTitleTest {

    public void shouldPutEveryCharactersToLowerCase() {
        assertThat(new EnglishTitle("ASimpleTitle").toUrlFriendlyString())

    public void shouldDeleteSpecialCharacters() {
        assertThat(new EnglishTitle("a!si?m=p'le#tit%l)e+").toUrlFriendlyString())

    public void shouldReplaceSpaceWithDash() {
        assertThat(new EnglishTitle("a simple title").toUrlFriendlyString())

    public void shouldReplaceAmpersandWithAnd() {
        assertThat(new EnglishTitle("The title of a nice & simple post !").toUrlFriendlyString())


Making a string URL friendly is not hard, it provides a clean URL and makes it SEO friendly as well.

(The whole project sources are available here)