How to append Trailing Slash to the requested URL

Splash Forums Rewrite Users How to append Trailing Slash to the requested URL

Tagged: ,

This topic contains 11 replies, has 5 voices, and was last updated by  Christian Kaltepoth 2 years, 7 months ago.

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #24577

    albertoivo
    Participant

    Hello,

    In my prettyfaces, I used to do this to append trailing Slash on all URLs:

    <rewrite match="^(?!.*\.xhtml.*).*" trailingSlash="append" redirect="301" />

    How can I do this with Rewrite?

    Thanks in advance.

    #24586

    Good question! To start, you can always use your existing prettyfaces configuration – just include the rewrite-config-prettyfaces JAR:

    <dependency>
        <groupId>org.ocpsoft.rewrite</groupId>
        <artifactId>rewrite-config-prettyfaces</artifactId>
        <version>2.0.4.Final</version>
    </dependency>

    But if you want to do things the Rewrite way, you can do this:

    .addRule(TrailingSlash.append)
    .when(Path.matches("/{x}"))
    .where("x").matches("^(?!.*\.xhtml.*).*$")

    I hope this helps 🙂

    #24590

    albertoivo
    Participant

    Totally works!
    Thank you very much!!!!

    #25020

    youngreymond
    Participant

    Hello, I’m trying to append Trailing Slash to the requested URL in the Rewrite way, but the physical page is showed instance of Join path.

    this is my configuration

    @Override
    	public Configuration getConfiguration(ServletContext context) {
    		return ConfigurationBuilder.begin()
    
    				.addRule(TrailingSlash.append())
    				.when(Path.matches("/{x}"))
    				.where("x").matches("^(?!.*\\.xhtml.*).*$")
    				
    				.addRule().when(Path.matches("/")).perform(Redirect.temporary(context.getContextPath() + "/web/home"))
    				.addRule().when(Path.matches("/web/")).perform(Redirect.temporary(context.getContextPath() + "/web/home"))
    				
    				.addRule(Join.path("/web/home").to("/faces/portal/home.xhtml"))
    				.addRule(Join.path("/web/login").to("/faces/portal/login.xhtml"))
    				
    				.addRule(Join.path("/web/{modulo}").to("/faces/portal/home.xhtml"))
    				.perform(PhaseOperation.enqueue(new IgnorePostbackOperation(Invoke.binding(El.retrievalMethod("sessionBean.processModuloSeleccionado")))).after(PhaseId.RESTORE_VIEW))
    				.where("modulo").bindsTo(PhaseBinding.to(El.property("sessionBean.selectedModuloValue")).after(PhaseId.RESTORE_VIEW))
    				
    				.addRule(Join.path("/web/{modulo}/{servicio}").to("/faces/portal/{modulo}/{servicio}.xhtml"))
    				
    		;
    
    	}

    and this is the page that I trying to get http://localhost:8080/portal/web/home/

    but I am getting this : http://localhost:8080/portal/faces/portal/home.xhtml/
    I tried putting the TrailingSlash Rule to the end of Rules but I get the same behavior, are there something wrong with my configuration ?
    I tried putting the / to the end to every Join.path but, the result is the same.

    #25022

    Hmmm. I’m not sure if TrailingSlash and Joins play nice together in their current configuration.

    What surprises me is:

    I tried putting the / to the end to every Join.path but, the result is the same.

    So you say the rewriting doesn’t work any more if you replace:

    .addRule(Join.path("/web/home").to("/faces/portal/home.xhtml"))

    With:

    .addRule(Join.path("/web/home/").to("/faces/portal/home.xhtml"))

    Or did I get something wrong here?

    #25024

    youngreymond
    Participant

    Hi,

    this is working fine now
    .addRule(Join.path("/web/home/").to("/faces/portal/home.xhtml"))

    but I can not automatically add the slash when
    .addRule(Join.path("/web/home").to("/faces/portal/home.xhtml"))
    or better said this
    http://localhost:8080/cmpPortal/web/home

    I would like to automatically add the slash to get this
    .addRule(Join.path("/web/home/").to("/faces/portal/home.xhtml"))
    or better said this
    http://localhost:8080/cmpPortal/web/home/

    this is the Rule to append the slash

    .addRule(TrailingSlash.append())
     .when(Path.matches("/{x}"))
     .where("x").matches("^(?!.*\\.xhtml.*).*$")
    #25025

    As I said, I’m not sure that TrailingSlash works together with Join URLs. But I may be wrong.

    To be honest, I’m not sure what you want to achieve. Isn’t it much easier to simply append the slash to all the Joins? Won’t this do what you want?

    #25662

    hcubyc
    Participant

    @up
    I’m pretty sure that what he wanted to achieve is to avoid double Join URLs, e.g.
    he wants (I want that as well) that
    .addRule(Join.path("/web/home/").to("/faces/portal/home.xhtml"))
    will both work with /web/home and /web/home/ so he won’t have to do that:

    .addRule(Join.path("/web/home/").to("/faces/portal/home.xhtml"))
    .addRule(Join.path("/web/home").to("/faces/portal/home.xhtml"))
    #25673

    I know this isn’t the optimal and nicest solution for your problem, but could you try this:

    .addRule(Join.path("/web/home{slash}").to("/faces/portal/home.xhtml"))
    .where("slash").matches("/?")
    #25674

    Perhaps we should think about adding trailing slashes as a feature to Join. Something like:

    .addRule(Join.path("/web/home").to("/faces/portal/home.xhtml").allowTrailingSlash())

    #25677

    hcubyc
    Participant

    @up
    The solution you gave works like a charm. Well I look forward to something like allowTrailingSlash(), because you sometimes user adds them, sometimes browser – you never know ; )

    #25678

    If you think this feature would be useful, just create an issue for that. 🙂

    https://github.com/ocpsoft/rewrite/issues

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

You must be logged in to reply to this topic.

Comments are closed.