在Go中自动301重定向

时间:2011-11-02 05:10:37

标签: redirect go

我正在尝试将网址重定向到其他网址(301或301)。基本上,我正在寻找相当于以下PHP代码:

<?php
// Permanent redirection
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://google.com/");
?>

到目前为止,这就是我所做的:

writer.Header().Set("Location", "http://google.com")
writer.WriteHeader(301)

还有:

handler := rawHttp.RedirectHandler("http://google.com", 301)
handler.ServeHTTP(writer, req)

它有点'有效'。唯一的问题是它写了一个“找到”文本,其中包含指向http://google.com的链接,所以我要点击它。我想让它自动重定向而不显示“找到”链接。

1 个答案:

答案 0 :(得分:1)

解决了它。要么有效。我在设置重定向之前只发送了一些文字。