How to Code a PHP Redirect - PHP Header Redirect

Redirection in PHP can be done using the header() function.

To setup, a simple redirect simply creates an index.php file in the directory you wish to redirect from with the following content:

# redirct to another website
<?php header('Location: http://www.redirect.to.url.com/'); ?>

Where http://www.redirect.to.url.com is the URL you wish the users to be redirected to.

We can also redirect to another page on the same domain and we can do it with the following content:

# redirct to another page on same website
<?php header('Location: /another-page'); ?>

Get in touch 👋

Feel free to email me about anything. Want some advice? Give some feedback?

You can also reach me around the web: GitHub, LinkedIn, Twitter, DEV, Facebook, Instagram, CodePen