Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java and SEO friendly URLs: ©reate ╨ a valid http URL from a string composed by special caracters

I'm trying to extract SEO friendly URLs from strings that can contain special characters, letter with accents, Chinese like characters, etc.
SO is doing this and it's translating this post title in

java-and-seo-friendly-urls-reate--a-valid-http-url-from-a-string-composed-by-s

I'm trying to do this in Java.
I'm using this post solution with URLEncoder.encode to translate Chinese and other symbols into valid URL characters.

Have you ever implemented something like this? Is there a better way?

like image 632
mickthompson Avatar asked Jun 23 '10 18:06

mickthompson


People also ask

How do I create a URL from a string?

In your Java program, you can use a String containing this text to create a URL object: URL myURL = new URL("http://example.com/"); The URL object created above represents an absolute URL. An absolute URL contains all of the information necessary to reach the resource in question.

Which of the given is are the valid guideline's to follow for SEO friendly URLs?

Use lowercase letters and standard characters SEO-friendly URLs support Google's guidelines for readability. That's why creating URLs that use lowercase letters and standard characters is a best practice for improving search engine rankings.

What is a friendly URL example?

Friendly URLs are called Aliases in Sitecore. The benefit of creating a friendly URL is that they are easier to remember and contain key words about the web page. Example: Original URL: https://portal.ct.gov/Services/Education/Higher-Education/Higher-Education-Information-and-Resources.


1 Answers

This might be an oversimplistic approach to the problem, but you could just use regular expressions to remove all non standard characters. So after converting your string to lowercase, you can replace all non lowercase alphabetic characters with an empty character and then replace all spaces with the '-' character.

private static String encodeForUrl(String input) {
  return input.toLowerCase().replaceAll("[^a-z\\s]", "").replaceAll("\\s", "-");
}
like image 112
Steve Bennett Avatar answered Sep 21 '22 01:09

Steve Bennett