How to Make a PHP Redirect Page
There are numerous reasons to redirect visitors to your website. For example, if you change the location of a page, it makes sense to replace the old file with a script to redirect visitors to the new page. If you want to display a message to visitors when they click a link away from your website, you can create a redirect page to achieve this purpose. In addition, if a visitor does not have permission to view a certain page on your website, a redirect can be used to display a "Permission Denied" page. PHP, the popular web programming language, can be used for this purpose.
Instructions
-
-
1
Open a text editor, such as Notepad, and create a new page. Use PHP's "header" function to send out the redirect HTTP header "Location: http://www.website-to-redirect-to.com." Ensure that there is no output before the HTTP header is sent, including white space outside the PHP tags, as it will cause an error. Here is the code for this step:
<?php
header("Location: http://www.examplesite.com");
-
2
If you want to display a message to users before redirecting them, use the "refresh: x" and "URL=http://www.someurl.com" headers to make the script wait "x" seconds before redirecting the user. After you send the header, you can display some text to the user. For example:
<?php
header( "refresh:5;URL=wherever.php" );
echo "You will be redirected in 5 seconds";
-
-
3
If you want to redirect users based on a "GET" parameter, a parameter in the URL, such as "redirect.php?URL=http://www.google.com," you can use PHP's "$_GET" array to retrieve the URL and redirect accordingly. Here is the code:
<?php
header("Location: " . $_GET['URL']);
-
4
Save the PHP file. Make sure that the file is saved with a ".php" extension, or else it will be interpreted as a text file and will not run. In Notepad this is done by choosing "File" in the menu bar, clicking "Save As," changing the "Save As Type" option to "All Files" and clicking "Save."
-
1