Your problem is that you print a content-header at the top of the file. The location header must be the first header. Only print the content header for the HTML, don't print it for the redirect. Also you should use CGI.pm's header() and redirect() methods rather than doing them by hand.