Q:

How to Make a Redirect in PHP

0

How to Make a Redirect in PHP

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the PHP header() Function

You can simply use the PHP header() function to redirect a user to a different page.

The PHP code in the following example will redirect the user from the page in which it is placed to the URL http://www.example.com/another-page.php. You can also specify relative URLs.

<?php
header("Location: http://www.example.com/another-page.php");
exit();
?>

If you want to redirect the users from old page to a new page on a permanent basis then also mention HTTP response code in the header() function as shown in the following example, so that search engines transfer "page rank" from the old page to the new page.

<?php
// 301 Moved Permanently
header("Location: http://www.example.com/another-page.php", true, 301);
exit();
?>

If the status code is not specified explicitly, for instance header("Location: URL") defaults to 302 (Found). For temporary redirect use the HTTP status code 307.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP  and MySQL Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to Strip All Spaces Out of a String in PHP... >>
<< How to Get the Current Date and Time in PHP...