Reply To: Redirect non-www URLs

Splash Forums Rewrite Users Redirect non-www URLs Reply To: Redirect non-www URLs

#27692

You could try something similar to this:


.addRule()
.when(Not.any(Domain.matches("www.myname.com")))
.perform(new HttpOperation() {
   @Override 
   public void performHttp(HttpServletRewrite event, EvaluationContext context) {

      String newUrl = event.getInboundAddress().toString()
               .replace("myname.com", "www.myname.com");

      Redirect.temporary(newUrl).performHttp(event, context);

   }
})

I didn’t test it, but it shows the idea. You can use the Domain condition, but the operation requires some custom code. Which certainly can be improved. 😉