Redirecting with PHP
When we installed our new central Download Handler software, we wanted to direct download links from old download handlers to the new one.
Most of the old download handlers were PHP. So I simply replaced the old download handler scripts with a script containing a PHP redirect to the new software.
I'll show you how I did it. And also give you additional information about redirecting
To redirect clicks to the new central download handler I replaced the old PHP script with:
<?php header("Location: /DownloadHandlerLink.php?file=myfile.zip"); ?>
"/DownloadHandlerLink.php" is the URL to the new Download Handler software. The URL may be relative or absolute http://... . "myfile.zip" is the name of the file being downloaded.
Links to the old download scripts at social sites and on your web pages can remain unchanged.
To avoid yet more lines in the .htaccess file, a PHP script can be replaced with code to redirect the browser to the new location.
Here is a recap of the format and additional details:
header("Location: URL");
Replace "URL" with the URL the browser is to be redirected to.
If more PHP code follows that header() command, put an exit command on the next line. It is to prevent the execution of the other PHP code. Like:
header("Location: URL"); exit;
The above format will redirect with status "302 Found", which is a temporary move status.
To redirect with status "301 Moved Permanently", use this format:
header("Location: URL", true, 301);
Replace "URL" with the URL the browser is to be redirected to.
The "true" (no quotes) parameter causes any earlier headers with the same name to be replaced with this one. The "301" (no quotes) parameter is the status code to send to the browser.
When a PHP script is moved or a different one is to be used at a different location, links to the old PHP script either need to be changed or a redirect implemented.
A redirect can be implemented without .htaccess lines by replacing the old PHP script with redirect code.
Will Bontrager