自动在Go中进行301重定向

huangapple go评论89阅读模式
英文:

Automatically 301 Redirecting in Go

问题

我正在尝试将一个URL重定向到另一个URL(无论是301还是302)。基本上,我正在寻找与以下PHP代码相对应的Go代码:

<?php
// 永久重定向
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的链接的“Found”文本,所以我必须点击它。我希望它在不显示“Found”链接的情况下自动重定向。

英文:

I'm trying to redirect a url to a different url (either 301 or 301). Basically, I'm looking for the go equivalent of following php code:

&lt;?php
// Permanent redirection
header(&quot;HTTP/1.1 301 Moved Permanently&quot;);
header(&quot;Location: http://google.com/&quot;);
?&gt;

So far this is what I've done:

writer.Header().Set(&quot;Location&quot;, &quot;http://google.com&quot;)
writer.WriteHeader(301)

And also:

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

It 'kind of' works. The only problem is it write a 'Found' text with a link to http://google.com so I've to click it. I want it to redirect automatically without displaying a 'Found' link.

答案1

得分: 1

解决了。两种方式都可以。我只是在设置重定向之前发送了一些文本。

英文:

Solved it. Either works. I was just sending some text before setting up the redirect.

huangapple
  • 本文由 发表于 2011年11月2日 13:10:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/7976056.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定