how to redirect to another page using cgi under apache

149 views
Skip to first unread message

Sammi

unread,
Nov 2, 2011, 3:50:57 AM11/2/11
to golang-nuts
Hey,

The cgi code cannot redirect to a new page.

fmt.Println("Location: http://google.com\n")


I knew in http package, there is a function Redirect

http.Redirect(w http.ResponseWriter, r http.Request, url string,
code int)

But how can I get the http.ResponseWrite from the cgi environment?

Any sample code or tips are welcome.


Thanks,
Sammi

Miki Tebeka

unread,
Nov 2, 2011, 11:19:20 AM11/2/11
to golan...@googlegroups.com
The following works for me:

package main

import (
    "fmt"
)

func main() {
    fmt.Println("Content-Type: text/plain")
    fmt.Println("Location: http://www.google.com")
    fmt.Println()
    fmt.Println("Moved to http://www.google.com")
}


Brad Fitzpatrick

unread,
Nov 2, 2011, 11:26:13 AM11/2/11
to Sammi, golang-nuts
Are you using the http/cgi package?  cgi.Serve takes a Handler like normal, which gives you a response writer.

Try ending your line with \r\n instead of \n, if you're not using the cgi package.

Sammi Song

unread,
Nov 2, 2011, 4:19:10 PM11/2/11
to golan...@googlegroups.com
package main

import (
    "fmt"
)

func main() {
    fmt.Println("Content-Type: text/plain")
    fmt.Println("Location: http://www.google.com")
    fmt.Println()
}

works for me now. It might be \r\n relative.

Sammi
Reply all
Reply to author
Forward
0 new messages