automatically adding trailing slashes to url's that "need" slash

Splash Forums PrettyFaces Users automatically adding trailing slashes to url's that "need" slash

This topic contains 2 replies, has 2 voices, and was last updated by  Serkan Durusoy 5 years, 10 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #18100

    Serkan Durusoy
    Participant

    Hi Everyone,

    We just began playing around with prettyfaces.

    I’m trying to create a standard approach to all my url constructing schemes – this is in regard to seo and some other requirements – and need some help.

    What I would like is that:

    A URL should be appended by a trailing slash unless:

    * contains the string xhtml

    * it already has a trailing slash

    * ends with any one of “jpg, jpeg, gif, png, js, css”

    I already have set up some mappings like:

    * /myapp/myseourl pattern to view: /myapp/mypage.xhtml

    And therefore the rewrite rule should not mess these up.

    I’m thinking, this looks like a regexp problem to be used in

    <rewrite match=”someregexp” trailingSlash=”append” />

    but I cannot quite put my finger on it and I’m pretty much a regexp newbie.

    I hope this information is sufficient for an answer.

    Thanks,

    Serkan

    #21717

    Hey Serkan,

    I think there are basically two ways to achieve this:

    First you could try to build the correct regular expression for the usecase. But this will be very difficult. What you will probably need is a negative lookahead which is a very advance regular expression feature. Take a look at this page for some details on this:

    Guide to Regular Expressions in Java (Part 2)

    You could also search this forum for “lookahead” and you will find some interesting posts. To test your regular expressions I recommend to take a look at this site:

    http://myregexp.com/

    BTW: You won’t have to implement the “it doesn’t already have a trailing slash” check in the regular expression because “trailingSlash” will do this for you.

    The second option to implement this behavior is to write a custom rewrite processor. Just use a rewrite rule that matches URLs and implement the behavior yourself:

    <rewrite processor="com.example.myapp.MyProcessor" />

    Now just implement the Processor interface and do whatever you want with the URL! :)

    See this part of the documentation for details:

    http://ocpsoft.com/docs/prettyfaces/3.3.2/en-US/html/inbound_rewriting.html

    I hope this helps! :)

    Christian

    #21718

    Serkan Durusoy
    Participant

    Hi Christian,

    Thank you for the very detailed response.

    Our main objective was to provide a consistent URL scheme and also the ability to accept incoming urls regardless of the trailing slash.

    Yahoo for example removes the trailing slashes from links, google keeps them.

    In the end, we decided to go with no trailing slashes at all and used:

    <rewrite match=”” trailingSlash=”remove”/>

    This way all urls like http://mydomain/myapp/somedir/somepath/ get 301 redirected to http://mydomain/myapp/somedir/somepath while we keep all our url-mappings and internal linking without the trailing slash.

    I guess this does the trick about “consistency” and solves the main problem.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

Comments are closed.