301 permanent Redirect

Splash Forums Rewrite Users 301 permanent Redirect

This topic contains 11 replies, has 3 voices, and was last updated by  banifou 9 months, 1 week ago.

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

    banifou
    Participant

    Hi guys,

    I have to 301 (permanent) redirect a bunch of urls to new ones.
    How can I perform this with rewrite. I’m already using OCPsoft Rewrite for my website to make urls bookmarkable.

    Thanks in advance
    banifou

    #27252

    Hi Banifou,

    Try something like:

    .when(Path.matches("/old/{path}"))
    .perform(Redirect.permanent("http://domain.com/new/{path}/etc/etc"));
    
    #27254

    banifou
    Participant

    Great! It’s working.
    On more question, I’ve a not so small list of url to redirect.
    My code looks like following:

    public class RewriteUrlConfig extends HttpConfigurationProvider {
    
        public Configuration getConfiguration(final ServletContext context) {
            return ConfigurationBuilder.begin()
          
           // many rules like this
          .addRule(Join.path("/home").to("/home.jsf"))
            .
            .
    
           // now added the redirect
           .addRule().when(Path.matches("/old/{path}"))
           .perform(Redirect.permanent("http://domain.com/new/{path}/etc/etc"));
    }
    }

    Is there a way to loop over the list and add all the redirects?
    Thanks

    #27255

    Sure, absolutely:

    
    Map<String, String> urlMap = ...
    for( String oldPath : oldPaths )
    {
       builder.addRule
          .when(Path.matches(oldPath))
          .perform(Redirect.to(urlMap.get(oldPath)))
    }
    

    Or however you want to implement the loop. Remember you can use {params} to simplify many mappings if your URL structure has patterns and repetition.

    #27256

    banifou
    Participant

    Perfect! Thank you Lincoln.

    #27262

    banifou
    Participant

    Which builder is this? I cannot find any builder from OcpSoft!?
    I tried to put inside of ConfigurationBuilder.begin() something like

            Iterator it = myMap.entrySet().iterator();
             while (it.hasNext()) {
                Map.Entry pair = (Map.Entry) it.next();
                builder.addRule().when(Path.matches(pair.getKey())
          .perform(Redirect.to(pair.getValue()))
    

    but it’s not working?

    • This reply was modified 10 months ago by  banifou.
    #27265

    Use ConfigurationBuilder like in the example here:

    http://www.ocpsoft.org/rewrite/#section-5

    #27278

    banifou
    Participant

    Hi,

    I read all the examples. I couldn’t find any example how to iterate over hashmap or loop over a list inside the ConfigurationBuilder (see my previous post).
    Thanks for your help.

    • This reply was modified 9 months, 2 weeks ago by  banifou.
    #27280

    Just iterate like you normally would, and call the builder.addRule() method from within your loop:

    
    loop ( over stuff )
    {
        builder.addRule()
          .when(Direction.isInbound().and(Path.matches(key)))
          .perform(Forward.to(value));
    }
    #27282

    banifou
    Participant

    Hmm, this is just not working, I’m sorry there is no such builder in ocpsoft rewrite. I’m using the last version 2.0.12.
    Here is my code:

    public Configuration getConfiguration(final ServletContext context) {
            return ConfigurationBuilder.begin()     
                    
            for (Map.Entry pair : linkName.entrySet()) {
                builder.addRule().when(Direction.isInbound().and(Path.matches("/some/{page}/"))).perform(Forward.to("/new-{page}/"));
            }
        }
    
    #27285

    Without too much commentary from me on why your code didn’t work, I’ll just say that can’t run code after you return from a function. Just assign the builder you are already using to a variable named “builder”. This should fix your code:

    public Configuration getConfiguration(final ServletContext context) {
           ConfigurationBuilder builder = ConfigurationBuilder.begin()     
                    
            for (Map.Entry pair : linkName.entrySet()) {
                builder.addRule().when(Direction.isInbound().and(Path.matches("/some/{page}/"))).perform(Forward.to("/new-{page}/"));
            }
    
            return builder;
        }
    #27286

    banifou
    Participant

    Finally, this works! It’s difficult sometimes without having documentation.
    Thanks Lincoln.

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

You must be logged in to reply to this topic.

Comments are closed.