In this tutorial, we’ll learn how to redirect one PHP page to another using PHP code.
Note: As an example, we are going to use http://example.com/source_page.php as a source page and we will do PHP redirect to page “destination_page.php” located at http://example.com/destination_page.php
Redirect using header() function:
You can use the header() function of PHP to redirect visitors from one page to another. It can be used like this:
Please put the below code in “source_page.php”:
Let’s illustrate this through GIFs:
Our file “source_page.php” looks like this:
Contents of “destination.php” are:
Let’s see how the code in source_page.PHP redirects us from http://example.com/source_page.phpto http://example.com/destination_page.php
Types of redirects:
301 Permanent Redirect:
The redirection with the code 301 is a permanent redirect. Please use this type of redirection when a web page has been permanently moved to another page or when you have closed your old website permanently and wish to redirect all the visitors to a new website. This kind of redirect will let search engines know that they need to start indexing the new website or a new webpage to which the old page has been redirected.
302 Temporary Redirect (or Found):
302 redirection is a temporary redirection, and it should be used when a webpage/website is to be redirected to another webpage/website temporarily. This type of redirection helps to keep your source webpage/website indexed in the search engines; thus keeping your SEO unaffected.
For example, when your webpage is undergoing maintenance, and you wish to redirect it to a maintenance page, you can use 302 redirections.
307 Moved Temporarily:
This is HTTP 1.1 successor of 302. This is similar to 302 redirects. The major difference is that 307 redirect keeps the HTTP method unchanged at the destination as well. E.g. If the source is sending “GET”, “GET” will be passed to the destination, unlike the 302 redirects. For SEO purposes, 302 redirects is preferred over 307.
How to implement these redirection types in PHP? s seen in the last section, each redirect type is associated with a status code like 301, 302, 307, etc. We can pass these codes to the header() function like this (please insert this in source_page.php):
This block of code will redirect source_page.php to destination_page.php with “301 permanent redirections”.
Let’s see how this works:
In the same way, you can implement 302 and 307 redirections. Please just change the status code to 302 or 307 in the PHP code.
Htaccess is a configuration file used on dedicated web servers like Apache, Nginx, etc. It may exist anywhere in a document root of a website (Document root is a directory path where the website files exist. E.g. Default document root on apache server is /var/www/html). It is not mandatory to create this file, but if it exists, it is loaded by the server whenever the website is accessed.
Please create .htaccess file within the document root of the website.
To perform 301 redirects from http://example.com/source_page_htaccess.php to http://example.com/destination_page_htaccess.php please add below redirection code to the .htaccess file:
Redirect 301 /source_page_htaccess.php http://www.example.com/destination_page_htaccess.php
Your .htaccess file should look like this:
Now, please browsehttp://example.com/source_page_htaccess.php, it should redirect to http://example.com/destination_page_htaccess.php like this:
Similarly, you can perform 302 or 307 redirections. You will just need to change the number 301 to 302 or 307 in the .htaccess redirection code above.
Note: If the redirection does not work, you may need to restart httpd/apache service on the server. On a few servers, .htaccess is disabled by default. You will need to enable that. Please refer to the next section for the instructions.
1. Open the configuration file for the website. Generally, it is located at “/etc/httpd/sites-available/”. E.g. For example.com, the file would be “/etc/httpd/sites-available/example.com.conf”
2. Inside the “VirtualHost” block, please add the below code:
Options Indexes FollowSymLinks
Require all granted
The final code will look like this:
Save the file and restart httpd/apache service using below command:
service httpd restart or service apache2 restart