You can redirect a URL in several ways. It is beneficial for those looking to redirect requests from their main domain to another directory under that domain. For instance, if you want to direct requests for example.com to example.com/blog, you can do this using an .htaccess file. But, it’s also possible to redirect by using PHP.
In this article, you will learn to create a redirect using PHP.
Does redirecting work with https (SSL certificate)?
The answer is ‘No’. Redirecting is only possible with ‘http’ URLs. In case you install an SSL certificate on ‘example.com’ and visit ‘https://example.com’, you will be redirected to the ‘Site Not Found’ page.
Steps to Create a Simple Redirect in PHP
Create a file with a name index.php. This can be done by using an FTP client such as Filezilla, or by using SSH. Check the below articles for instructions on how to create it (based on if you’re using an FTP client or SSH):
How to Create and Edit a File via FTP?
How to Create and Edit a File via SSH?
Ensure that the file is in the domain’s directory. This directory is the one that is mentioned while adding the domain to your panel. This can also be confirmed by going to Manage Domains and clicking the Edit button to the right of the domain.
In this index.php file, include the below line of code:
<?php header("Location: http://www.example.com/blog"); ?>
Don’t forget to change the sample URL to the URL you would like the site to forward.
After this you will get a simple and quick redirection that you can use to easily redirect an HTTP request to any URL.