Create Rule to Redirect\Change URL in the Address Bar of the Browser

Splash Forums Rewrite Users Create Rule to Redirect\Change URL in the Address Bar of the Browser

This topic contains 2 replies, has 2 voices, and was last updated by  RB 2 minutes ago.

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

    RB
    Participant

    Is it possible to redirect/change the url in the address bar of the browser? I want that if the user type ” /Recommended.xhtml?recd=teste ” on the address bar it got redirected to ” /recommended/teste “. I want only ” /recommended/teste ” to be visible on the address bar of the browser even if the user directly input ” /Recommended.xhtml?recd=teste “. The code below is working but it doesn’t change the link in the address bar.

    This is what I got so far:

    import javax.servlet.ServletContext;
    
    import org.ocpsoft.logging.Logger.Level;
    import org.ocpsoft.rewrite.annotation.RewriteConfiguration;
    import org.ocpsoft.rewrite.config.Configuration;
    import org.ocpsoft.rewrite.config.ConfigurationBuilder;
    import org.ocpsoft.rewrite.config.Direction;
    import org.ocpsoft.rewrite.config.Log;
    import org.ocpsoft.rewrite.servlet.config.Forward;
    import org.ocpsoft.rewrite.servlet.config.HttpConfigurationProvider;
    import org.ocpsoft.rewrite.servlet.config.Path;
    import org.ocpsoft.rewrite.servlet.config.Redirect;
    import org.ocpsoft.rewrite.servlet.config.rule.Join;
    
    @RewriteConfiguration
    public class ApplicationConfigurationProvider extends HttpConfigurationProvider
    {
       @Override
       public Configuration getConfiguration(ServletContext context)
       {
          return ConfigurationBuilder.begin()
        		  .addRule()
      		  		.when(Direction.isInbound().and(Path.matches("/Recommended.xhtml?recd={recommended}")))
      		  		.perform(Redirect.temporary("/recommended/{recommended}"))
      		  	  .addRule()
      		  	  	.when(Direction.isOutbound().and(Path.matches("/Recommended.xhtml?recd={recommended}")))
      		  	  	.perform(Redirect.temporary("/recommended/{recommended}"))
        		  .addRule(Join.path("/recommended/{recommended}").to("/Recommended.xhtml?recd={recommended}"))
        		  .addRule(Join.path("/recommended/id/{id}").to("/Recommended.xhtml?id={id}"));
       }
    
       @Override
       public int priority()
       {
          return 10;
       }
       
    }

    In the web.xml

    <listener>
       <listener-class>org.ocpsoft.rewrite.servlet.impl.RewriteServletRequestListener</listener-class>
    </listener>
    
    <listener>
       <listener-class>org.ocpsoft.rewrite.servlet.impl.RewriteServletContextListener</listener-class>
    </listener>
    
    <filter>
       <filter-name>OCPsoft Rewrite Filter</filter-name>
       <filter-class>org.ocpsoft.rewrite.servlet.RewriteFilter</filter-class>
       <async-supported>true</async-supported>
    </filter>
    <filter-mapping>
       <filter-name>OCPsoft Rewrite Filter</filter-name>
       <url-pattern>/*</url-pattern>
       <dispatcher>FORWARD</dispatcher>
       <dispatcher>REQUEST</dispatcher>
       <dispatcher>INCLUDE</dispatcher>
       <dispatcher>ASYNC</dispatcher>
       <dispatcher>ERROR</dispatcher>
    </filter-mapping>
    #27454

    You can use .withInboundCorrection() on a Join for this.

    Also note that should not use query parameters in the .to() part of your rule. Path parameters are automatically transformed in a query parameter with the name of the parameter. So the rule should look like this:

    .addRule(
      Join.path("/recommended/{recd}").to("/Recommended.xhtml").withInboundCorrection()
    )
    

    I hope this helps.

    #27457

    RB
    Participant

    Thanks Christian! It worked like a charm!

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

You must be logged in to reply to this topic.

Comments are closed.