Rewriting problem

Splash Forums PrettyFaces Users Rewriting problem

This topic contains 3 replies, has 2 voices, and was last updated by  Lincoln Baxter III 10 years, 10 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
  • #17886


    Hello everybody!

    I have a problem using url rewriting for my current project.

    What i want is that the user can enter a URL like:

    The URL should internally be mapped to


    While it does work if i add a url-mapping for each page, that’s definitely not a satisfactory solution.

    I also tried using rewrite. Something like:

    <rewrite match=”/(.*)” substitute=”/faces/public/$1.xhtml” redirect=”chain”/>

    But as you can imagine, this is like an infinite rewrite-loop.

    Does anyone have any suggestions for this case?

    Thank you in advance!

    Best Regards,



    You probably want to use a negative lookahead to prevent the infinite loop:

    Guide to Regular Expressions in Java (Part 2)

    <rewrite match="/(?!.*\.xhtml.*)(.*)" substitute="/faces/public/$1.xhtml" redirect="chain" outbound="false"/>

    You may or may not need the \. or . depending on your requirements.

    You probably also want another rule set up to fix URLs that point to XHTML files. My example might cause an infinite loop, in which case we are working on updating this regex engine to support “stops” so that when a certain condition has been reached, rewriting is halted. Right now prettyfaces works much better with <url-mappings>, so you should consider using DynaView instead:

    // to fix inbound URLs pointing to XHTML files

    <rewrite match="/faces/public/(.*).xhtml" substitute="/$1" redirect="301"/>

    //using dynaview instead of rewrite:

    <url-mapping id="global">
    <pattern value="/*" />
    <view-id> #{bean.getViewPath} </view-id>

    public class Bean {

    public String getViewPath() {

    URL url = PrettyContext.getCurrentInstance().getRequestURL();
    String viewId = url.toString();
    if(viewId.endsWith(".xhtml") return viewId;
    else return viewId + ".xhtml";





    Thank you for your answer Lincoln. I’ll try to implement it using dynaview. What i still have to do is provide the Bean with a list of all pages (e.g. “welcome”, “contact”, … I have ~5-6 of them). Otherwise, all requests to resources like javascript or css would be changed to a .xhtml-URL.

    But anyways, injecting a List of Strings into the bean is easier than having to add a <url-mapping> for every page. Especially if the list can be generated using a database.

    Thanks for your advice :)


    No problem! Dynaview will intercept whatever is in the pattern, so be careful ;)

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

The forum ‘PrettyFaces Users’ is closed to new topics and replies.

Comments are closed.