英文:
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:
<?php
// Permanent redirection
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://google.com/");
?>
So far this is what I've done:
writer.Header().Set("Location", "http://google.com")
writer.WriteHeader(301)
And also:
handler := rawHttp.RedirectHandler("http://google.com", 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论