HTTP Redirect in PHP

Dani 0 Tallied Votes 3K Views Share

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.

<?php

// 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);
}
else
{
	header('HTTP/1.1 301 Moved Permanently', true, 301);
}


// Execute the redirect
header("Location: https://www.example.com/foo.html");

// Don't process or spit out anything in the script after this
die();
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: http://www.redirect.to.url.com/"); ?>

Be a part of the DaniWeb community

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