skip to Main Content

I recently set up a website and pushed it to production using Digital Ocean. However, I noticed that for both SEO purposes and to make Facebook Share work appropriately, I should set up my server to redirect www. requests to non-www. I’m running Play! Java 2.3 with a PostgreSQL database and the default Netty server. Any advice would be greatly appreciated.

2

Answers


  1. The right way to do it is to do in not on the framework/webserver side, but on the DNS-server side.

    You can do it in DNS-management area of GoDaddy or any other domain name registrar.

    Login or Signup to reply.
  2. There are lots of ways of redirecting. I wouldn’t say DNS-redirects are the correct and only way of doing it, it’s one way. Google is just fine with you doing a 301 redirect with Play.

    Here’s one way of accomplishing it with Play! filters (scala):

    object NonWwwFilter extends Filter {
    
      def apply(f:RequestHeader => Future[Result])(rh: RequestHeader): Future[Result] =
        if (rh.host.startsWith("www.")) {
          Future.successful(Results.MovedPermanently("https://" + rh.host.substring(4) + rh.uri))
        } else {
          f(rh)
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search