HTTP Redirect in PHP


If you need your PHP script to redirect to a different website, you can send an HTTP header to do that.

Remember, header() must be called before any actual output is sent, which includes not just HTML, but blank lines, etc.. as well.

About the Author

I'm a super-geeky programmer with a passion for Internet marketing. :)


// Tell the web browser that the page has been moved to a new location

if (strpos(PHP_SAPI, 'cgi') === 0)
	// If CGI interface between web server and PHP
	header('Status: 301 Moved Permanently', true);
	header('HTTP/1.1 301 Moved Permanently', true, 301);

// Execute the redirect

// Don't process or spit out anything in the script after this
monicachortle13 0 Newbie Poster

That was a big help. Thank You so much!

bangalore.webguru -9 Bangalore Webguru

Redirection in PHP can be done using the header() function. To setup a simple redirect, simply create an index.php file in the directory you wish to redirect from with the following content: < ?php header("Location:"); ?>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.