Strona główna > Java > java seo friendly url (slug)

java seo friendly url (slug)

Tworząc aplikację internetową, często w adresach pojawia się jakiś tekst, np tytuł wprowadzony przez użytkownika podczas dodawania artykuły, chociażby blogi na wordpressie.
Jednak adres w postaci http://www.ryznar.eu/artykul/śmieszny-artykuł podobno jest gorzej odbierany przez google, dwa w niektórych przeglądarkach jest on niezbyt czytelny bo przeglądarka nie potrafi przetworzyć np.: IE.
Dlatego tekst wprowadzony przez użytkownika należy nieco zmodyfikować. Z racji ze potrzebuję takiej funkcji napisałem sobie odpowiednią klasę:

import java.text.Normalizer;
import java.text.Normalizer.Form;

public class Slug {

    public static String parse(String input) {
        if (isEmptyOrNull(input)) {
            return "";
        }
        String out = normalize(input);
        out = removeDuplicateWhiteSpaces(out);
        out = out.toLowerCase();
        out = out.replace(" ", "-");

        return out;
    }

    private static String normalize(String input) {
        String result = Normalizer.normalize(input, Form.NFD).replaceAll("[^\\p{ASCII}]", ""); // 1
        result = result.replaceAll("[^a-zA-Z0-9\\s]", " "); // 2

        return result;
    }

    private static String removeDuplicateWhiteSpaces(String input) {
        return input.replaceAll("\\s+", " ");
    }

    private static boolean isEmptyOrNull(String input) {
        return (input == null || input.trim().length() == 0);
    }
}

1) tekst jest normalizowany, tzn znaki diakrytyczne są zmieniane na znaki ASCII
2) tutaj zmieniamy wszystkie znaki niealfanumeryczne na spacje

Możemy przetestować klasę:

        String inputText = "123-test       nąśŹA       zima :) !! #@ zła";
        System.out.println(inputText);
        System.out.println(Slug.parse(inputText));

wynik: 123-test-nasza-zima-za – zjadło literkę „l”
okazuje się że na l może być zamieniony a nie ł
Niestety w sieci nie widzę żadnej wskazówki jak ładnie rozwiązać ten problem, więc na razie pozostaje mi tylko zmodyfikować ciało funkcji normalize:

        String result = input.replace("ł", "l").replace("Ł", "L");

        result = Normalizer.normalize(result, Form.NFD).replaceAll("[^\\p{ASCII}]", ""); // 1
        result = result.replaceAll("[^a-zA-Z0-9\\s]", " "); // 2

jeśli ktoś wie jak to zrobić normalnie a nie poprzez takie kombinacje, będę wdzięczny za komentarz

About these ads
Kategorie:Java Tagi: , , ,
  1. 20 Grudzień 2012 o 16:52

    Howdy I am so glad I found your blog, I really found you by accident, while I was looking on Askjeeve for something else,
    Anyways I am here now and would just like to say thanks a lot for a
    tremendous post and a all round enjoyable blog (I also love the theme/design), I don’t have time to browse it all at the moment but I have book-marked it and also added your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the fantastic job.

  2. 6 Styczeń 2013 o 7:28

    This is a topic that is close to my heart… Best wishes! Exactly where are
    your contact details though?

  3. 14 Styczeń 2013 o 21:49

    Hey there, You’ve done a fantastic job. I’ll definitely digg it
    and personally suggest to my friends. I’m sure they’ll be benefited from this site.

  4. 29 Styczeń 2013 o 10:31

    Marvelous, what a blog it is! This website presents helpful
    information to us, keep it up.

  5. 8 Kwiecień 2013 o 18:44

    I have fun with, lead to I discovered just what I was taking a look for.
    You’ve ended my 4 day long hunt! God Bless you man. Have a nice day. Bye

  6. seo
    10 Kwiecień 2013 o 8:22

    What i do not understood is actually how you are now not actually much
    more well-liked than you may be right now. You are so intelligent.
    You realize therefore significantly on the subject
    of this topic, produced me individually believe it from numerous varied angles.

    Its like men and women are not interested unless it is something to do with Lady gaga!
    Your own stuffs outstanding. Always handle it up!

  7. 11 Kwiecień 2013 o 3:05

    Write more, thats all I have to say. Literally, it seems as
    though you relied on the video to make your point.
    You definitely know what youre talking about, why waste your
    intelligence on just posting videos to your blog when you could be giving us
    something enlightening to read?

  8. 13 Kwiecień 2013 o 6:36

    I love it when folks come together and share ideas. Great site, keep
    it up!

  9. 13 Kwiecień 2013 o 20:22

    I really love your website.. Pleasant colors & theme. Did you
    make this website yourself? Please reply back as I’m hoping to create my own website and want to know where you got this from or what the theme is named. Thanks!

  10. 13 Kwiecień 2013 o 20:41

    I pay a visit each day a few blogs and blogs to read content, except
    this webpage presents quality based writing.

  11. 14 Kwiecień 2013 o 3:23

    We stumbled over here from a different website and thought I should check things out.
    I like what I see so now i am following you. Look forward to
    checking out your web page again.

  12. 21 Kwiecień 2013 o 4:26

    This is a topic which is near to my heart… Best wishes!
    Where are your contact details though?

  13. 21 Maj 2013 o 7:35

    Great beat ! I would like to apprentice whilst you amend your web site, how can i subscribe for a blog website?

    The account helped me a acceptable deal. I were a little bit familiar
    of this your broadcast offered bright clear idea

  14. 2 Czerwiec 2013 o 2:10

    The clownfish are hermaphrodites, which means they develop into males
    first, and when they mature, they tend to change as females.
    There should be live rock and sand inside the tank. The less
    intense light they have, such as a fluorescent light, the less amount of algae will grow on the glass and decorations.

  15. 6 Czerwiec 2013 o 22:41

    Brainpulse is a top SEO Company India, which ensures
    that the search engine rankings of the web page fall within the top 10.
    Look for a company that makes a certain amount of effort in knowing your business, what you wish to achieve from your web presence etc.
    This has said to be effective in reducing paper
    waste by 90%.

  16. 30 Czerwiec 2013 o 14:54

    howdy! , I like the creating hence a whole lot! discuss most people talk more about this page in America online? I would like a professional within this area to uncover this challenge. Maybe which is anyone! Eager for help you.

  17. 9 Lipiec 2013 o 23:59

    What’s up everyone, it’s my first pay a quick visit at this site,
    and piece of writing is in fact fruitful designed for me, keep up posting these content.

  18. 21 Lipiec 2013 o 16:50

    People go for crash dieting is bad in order to function
    normally. As for trends in Dieting Is Bad, it’s hard to know what’s truly changing here.
    This is TRUE because the body is sent into starvation mode because it thinks
    it’s in a period of time. Before, I used Spark People, which is said to prolong health, stave off diagnosis, and it would be almost impossible to drop out.

  19. 12 Sierpień 2013 o 3:03

    Woah! I’m really enjoying the template/theme of this site. It’s
    simple, yet effective. A lot of times it’s challenging to get that „perfect balance” between user friendliness and visual appeal. I must say you’ve
    done a superb job with this. Additionally, the blog loads super fast for me on Internet explorer.
    Excellent Blog!

  20. 28 Marzec 2014 o 22:10

    Hi! This post could not be written any better! Reading through this post reminds me of my previous room
    mate! He always kept talking about this. I will forward
    this page to him. Pretty sure he will have a good read. Thanks for
    sharing!

  21. 14 Kwiecień 2014 o 17:53

    A person essentially assist to make critically
    articles I might state. That is the very first time I frequented your web
    page and up to now? I surprised with the analysis you made to make
    this actual put up amazing. Great process!

  22. 23 Kwiecień 2014 o 18:32

    Hi there, after reading this amazing piece of writing i am
    too happy to share my knowledge here with friends.

  23. 18 Maj 2014 o 4:53

    If you elect to download iPhone flick you’ll need MP4 codec.
    You can jailbreak iOS 4.0.2 via Redsn0w and 4.0.1 can be jailbroken via jailbreakme 2.0.

    Generally there is more than one strategy to jailbreak.

  24. 20 Maj 2014 o 5:06

    Some could be presented as presents out of your large neighbors.
    Cultural Location on Facebook is just a simple
    online game much like Simcity. But it is only recommended by me for adventurous and the courageous.

  25. 24 Maj 2014 o 22:29

    For more farming fun: click the link a subscription to my posts.
    Both Cash Puppies come with a few rules that don’t connect with
    their Plantation Cash competitors. Let’s possess a glance at the things they are.

  26. 27 Maj 2014 o 5:22

    Just stated repositories are sources or spots where you could download software
    applications. Is your iPhone Mobile 4 caught with the
    spinning wheel to the Apple logo?

  27. 30 Maj 2014 o 0:34

    This game is a must for Boggle or Scrabble lovers. Enabling this feature will permit you
    to instantly reply with out quitting the existing page.
    You can easily find it in iTunes Application Store.

  28. 30 Maj 2014 o 7:17

    These games could teach them anything or two, even though
    they don’t understand it. Make sure most of the routes are very different however the treasure in the long run is the
    same.

  29. 30 Maj 2014 o 16:18

    One of the tools therefore mentioned is the jailbreak application that was lamera1n. You could add (or remove) repos within Cydia.
    This series of city places would be the campaign’s beginning.

  30. 31 Maj 2014 o 8:17

    Within this criticism we suppose that you pull straight from lifestyle or from the suitable
    image. Do not overlook, the arabesque includes the whole
    of the top such as the hair.

  31. 10 Czerwiec 2014 o 14:42

    Hello there! This is my first comment here so I just wanted to give
    a quick shout out and say I genuinely enjoy reading through your
    articles. Can you suggest any other blogs/websites/forums that
    cover the same topics? Thanks for your time!

  32. 30 Lipiec 2014 o 17:10

    Great article.

  33. 5 Sierpień 2014 o 17:14

    Nice post. I learn something totally new and challenging on websites
    I stumbleupon everyday. It will always be helpful to read through content from other authors and practice something from their sites.

  34. 6 Sierpień 2014 o 10:56

    Toller Post. Genau so unterhaltsam, wie das Gaming auch.
    So verrissen Computerspiele auch hingestellt wird, so ist psychologisch bewiesen worden, dass Gaming, ein dem Alter des
    Gamers angemessenes Game vorausgesetzt, förderlich
    für das Reaktionsvermögen ist. Beim Spielen lernt der Gamer
    fernab vom Alltag in erhöhter Frequenz zu Reagieren und zwischen wichtig und unwichtig zu unterscheiden. Viele
    Spiele vermitteln zusätzlich noch Kenntnisse über Wirtschaftlichkeit und fördern die Logik.
    Auch die verpönten MMOs können doch häufig den vorgeworfenen Effekt
    des Abdriftens in eine virtuelle Realität} aufhalten. Der Spieler mag mitunter vielen Freaks begegnen, jedoch findet der Spieler häufig in seinem Lieblingsspiel Gleichgesinnte.

    Kurz gehalten: das Gaming ist das Beste überhaupt!
    Ungeachtet des Games und der Plattform, Spielen ist ein Kulturgut.

  35. 10 Sierpień 2014 o 17:30

    Light this time is reflected on inclusions positioned parallel with
    each other. The shapes of designs also have meanings and
    symbolism such as the elephant with wealth and luck.
    Chrysoberyl This gemstone is arguably the most popular gemstone for its cats
    eye inclusions.

  1. No trackbacks yet.

Dodaj komentarz

Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:

Logo WordPress.com

Komentujesz korzystając z konta WordPress.com. Log Out / Zmień )

Zdjęcie z Twittera

Komentujesz korzystając z konta Twitter. Log Out / Zmień )

Facebook photo

Komentujesz korzystając z konta Facebook. Log Out / Zmień )

Google+ photo

Komentujesz korzystając z konta Google+. Log Out / Zmień )

Connecting to %s

Obserwuj

Otrzymuj każdy nowy wpis na swoją skrzynkę e-mail.

Dołącz do 91 obserwujących.

%d bloggers like this: