gpt4 book ai didi

java - 使用 Play 2.0.x 永久重定向

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:30:02 26 4
gpt4 key购买 nike

我想知道如何在 Play 框架 2.0.x 中为子域执行永久重定向 301。例如:www.example.com/* 重定向到 example.com/*。以前有人试过这个吗?

最佳答案

Global object将允许您intercept the request .出于显而易见的原因,您应该使用 GET 请求来执行此操作(即出于 SEO 目的),但其他请求(如 POST、PUT 等)应该从一开始就在您的 View 中正确创建。

另一方面,如果它只是一些为生活生产提供常见 HTML 页面的应用程序,请考虑在其前面使用一些 HTTP 服务器 - 然后您可以通过一些重写规则来实现这一点。

import play.GlobalSettings;
import play.mvc.Action;
import play.mvc.Http;
import play.mvc.Result;

import java.lang.reflect.Method;

public class Global extends GlobalSettings {

@Override
public Action onRequest(final Http.Request request, Method method) {
if ("GET".equals(request.method()) && "www.example.com".equals(request.host())) {
return new Action.Simple() {
public Result call(Http.Context ctx) throws Throwable {
return movedPermanently("http://example.com" + request.path());
}
};
}
return super.onRequest(request, method);
}
}

关于java - 使用 Play 2.0.x 永久重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13632778/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com