1

hi i'm using pretty faces 3.3.3 in tomcat 7

and this config

<rewrite match="/browse" trailingSlash="append" toCase="lowercase" />
<url-mapping id="browsecategory">
    <pattern value="/browse/" />
    <view-id value="/browser.xhtml" />
</url-mapping>

i want requests without trailing slash after "browse" to be redirected to browse/ (with trailing slash). The background: if the trailing slash is missing, my relative outputLinks are not handeled as subdirectorys, but as files in the current directory.

when i request now

localhost:8081/App/browse 

my browser gets into a redirect loop

EDIT:

is it possible that browse is a reserved keyword? when i replace it with squirrel everything works as expected:

<rewrite match="/squirrel" trailingSlash="append" toCase="lowercase" />
<url-mapping id="browsecategory">
    <pattern value="/squirrel/" />
    <view-id value="/browser.xhtml" />
</url-mapping>
wutzebaer
  • 14,365
  • 19
  • 99
  • 170
  • Hi! I am facing a similar issue. What happens during your redirect loop? In my case, the browser appends /index.jsp multiple times before stopping. – A M Dec 09 '14 at 23:39

2 Answers2

1

The problem is that your trailingSlash rewrite rule also matches things like /browse. Could you try to adjust it like this:

<rewrite match="^/browse$" trailingSlash="append" toCase="lowercase" />

I think this should work, because the rule will now only match exactly /browse and not /browse/.

chkal
  • 5,598
  • 21
  • 26
  • thanks, tried this, but still takes me to http://localhost:8081/App/browse//////////////////// – wutzebaer Apr 30 '13 at 12:30
  • I find it surprising that this would be the case. Have you fully redeployed the application? – Lincoln Apr 30 '13 at 19:38
  • yes =( do you have an small project where you could try it out? – wutzebaer Apr 30 '13 at 22:05
  • I don't currently. I could set one up but taking a break for the night. If you head over to the support forums you can upload an example project that reproduces this and I'll take a look tomorrow: http://ocpsoft.org/support/ – Lincoln May 01 '13 at 00:39
1

Because of the amount of confusion that has occurred using the <rewrite/> tag in PrettyFaces, we've migrated to a new core architecture for PrettyFaces (//Rewrite 2.0.0.Final) that provides much greater control over application configuration. (Available here http://ocpsoft.org/prettyfaces/)

I would recommend trying PrettyFaces 4 if your environment permits. You can leave your URL-mappings in the pretty-config.xml file if you wish, but you can now define more custom Rewrite rules, more safely, in a Rewrite ConfigurationProvider:

<!-- for JSF 2.x -->
<dependency>
    <groupId>org.ocpsoft.rewrite</groupId>
    <artifactId>rewrite-servlet</artifactId>
    <version>2.0.0.Final</version>
</dependency>
<dependency>
    <groupId>org.ocpsoft.rewrite</groupId>
    <artifactId>rewrite-config-prettyfaces</artifactId>
    <version>2.0.0.Final</version>
</dependency>

Leave your pretty-config.xml as it is:

<url-mapping id="browsecategory">
    <pattern value="/browse/" />
    <view-id value="/browser.xhtml" />
</url-mapping>

Now also create a ConfigurationProvider to handle your trailing slashes:

public class RewriteConfig extends HttpConfigurationProvider
{
   @Override
   public int priority()
   {
     return 10;
   }

   @Override
   public Configuration getConfiguration(final ServletContext context)
   {
     return ConfigurationBuilder.begin()
       .addRule()
         .when(Direction.isInbound().and(Path.matches("/{p}")))
         .perform(Redirect.to(context.getContextRoot() + "/{p}/"))
         .where("p").matches("^.*[^/]$");
    }
}

Don't forget to register/activate the ConfigurationProvider.

Additionally, you can do your URL-mappings in this configuration file as well, thus removing the need for pretty-config.xml or the PrettyFaces 4 con:

public class RewriteConfig extends HttpConfigurationProvider
{
   @Override
   public int priority()
   {
     return 10;
   }

   @Override
   public Configuration getConfiguration(final ServletContext context)
   {
     return ConfigurationBuilder.begin()

       .addRule(Join.path("/browse/").to("/browser.xhtml"))

       .addRule()
         .when(Direction.isInbound().and(Path.matches("/{p}")))
         .perform(Redirect.to(context.getContextRoot() + "/{p}/"))
         .where("p").matches("^.*[^/]$");
    }
}

I didn't test the regular expression in the matches() clause, but it should be something like that. I hope this helps!

Lincoln
  • 3,151
  • 17
  • 22
  • thanks, is version 4 available in maven? i just can find 3.3.3, and by the way, have you any idea why it isn't possible with 3.3.3? is my config wrong? or can there be a conflict with spring or primefaces? – wutzebaer Apr 30 '13 at 22:04
  • ps: i use servlet 3 and have no config in my web.xml – wutzebaer Apr 30 '13 at 22:11
  • Hey, sorry. Yes. Version 4 is available in Maven - just follow the instructions here: http://ocpsoft.org/prettyfaces/ – Lincoln May 01 '13 at 00:38
  • can you give me a more exact link? the default maven repository doesn't contain version 4 – wutzebaer May 02 '13 at 13:58
  • Sorry for the confusion, PrettyFaces 4 is Rewrite 2.0.0 - http://search.maven.org/#artifactdetails%7Corg.ocpsoft.rewrite%7Crewrite-config-prettyfaces%7C2.0.0.Final%7Cjar – Lincoln May 02 '13 at 17:32
  • Also, if you follow the instructions in my answer (see the POM snippit,) that will do what I just said :) – Lincoln May 02 '13 at 17:33