在 PlayFramework 1.2.x 中将 example.com 重定向到 www.example.com [英] Redirect example.com to www.example.com in PlayFramework 1.2.x

查看:76
本文介绍了在 PlayFramework 1.2.x 中将 example.com 重定向到 www.example.com的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 PlayFramework 1.2.x

I'm using PlayFramework 1.2.x

我想将我的网址 example.com 重定向到 www.example.com

I want to redirect the my url example.com to www.example.com

我唯一能想到的就是在 application.conf

The only thing I could think of was to set this in the application.conf

 prod.application.baseUrl=http://www.example.com/

但这不会导致 mydomain.com/some-page 被重写为 www.mydomain.com/some-page

But this doesn't lead to mydomain.com/some-page being rewritten as www.mydomain.com/some-page

这在 PlayFramework 1.2.x 中是否可行?

Is this possible within PlayFramework 1.2.x?

附言我可以通过重定向到 www.example.com 使其适用于根 url,但我需要使其适用于所有其他页面,并且重定向到其他页面的特定 URL 并不实际.

P.S. I can make it work for the root url by redirecting to www.example.com, but I need to make this work for all the other pages, and it's not really practical to redirect to a particular URL for the other pages.

将问题编辑为

推荐答案

您想将 url example.com 重定向到 www.example.com.

You want to redirect url example.com to www.example.com.

方法 1:

控制器

  @With(CheckUrl.class)
  public  class Application extends Controller{
        public static Result index() {
                return ok(index.render("Unable to resolve host."));
            }
    }

CheckUrl.java

CheckUrl.java

public class CheckUrl extends play.mvc.Action.Simple {

    public F.Promise<SimpleResult> call(Http.Context ctx) throws Throwable {

        String host = request().host();
        System.out.println("HOST IS "+host);

              if (host.equalsIgnoreCase("example.com")) {

             return F.Promise.pure(redirect("http://www.example.com"));

        }

}

因此,当用户每次在浏览器中点击 url 时,它都会从控制器发送到 CheckUrl.java 并重新发送到 wwwdot.所以这不是最干净的方法

So when everytime user hits the url in browser it will be send from controller to CheckUrl.java and redired to wwwdot.It not the cleanest approach so

方法 2:标准方法

Source1,Source2

这篇关于在 PlayFramework 1.2.x 中将 example.com 重定向到 www.example.com的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆