Get your next remote job on LaraJobs.
1 contributor Edit on GitHub PHP

Effortlessly redirect to another page using PHP

Table of contents:

Effortlessly redirect to another page using PHP

How to perform a redirection

To redirect to another page using PHP, run the following code:

// We make sure no text is echoed before we set the header.
 
header('Location: https://example.com');
 
exit;

Understanding the code

  1. We make sure no text has been echoed before setting the header. We don’t want to pollute our response. Continue reading if you want to know what I’m talking about.
  2. Then, using the header() function, we set a header that will inform the visitor’s browser that its user needs to be redirect to another page. What’s a header? I’ll explain later.
  3. Finally, we stop the execution of the code using exit. This isn’t mandatory, but this isn’t necessary either. It’s just common practice.

Basically, using HTTP, the server running your PHP code respond to your visitor’s browser and asks it to redirect its user to the URL you provided.

The anatomy of a HTTP response

Let me show you an example HTTP response, which is how a web server communicates with the browser:

HTTP/1.1 302 Found
 
Content-Type: text/html
Location: https://example.com
 
<html>
<p>This is a redirection.</p>
</html>

Weird, right? But this is easy. Here’s how a response is constructed:

  1. The status line containing:
    • The version of HTTP used by the server.
    • The status code.
    • The reason phrase for the status code.
  2. The headers. We can see our Location header holding the URL to which we want to redirect the visitor.
  3. The body, carrying the HTML of your web page. For a redirect, HTML is usually not needed, though.

Also, do you remember when I talked about not polluting our response? Well, echoing text before using the header() in PHP will add whatever text you like in it, which can have side effects.

301 or 302? How to choose the kind of redirection with PHP?

The header() function in PHP lets you choose which kind of redirect you perform. By default, it’s creates a 302 Found redirection:

header('Location: https://example.com');

Here’s the HTTP response sent on my local machine:

HTTP/1.1 302 Found
Server: nginx/1.25.1
Date: Tue, 19 Sep 2023 18:08:31 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: close
X-Powered-By: PHP/8.2.10
Location: https://example.com

To make it a 301 Moved Permanently kind of redirection, you can leverage the header() function third argument: $response_code:

header('Location: https://example.com', response_code: 301);

Which sends a HTTP response that looks like this:

HTTP/1.1 301 Moved Permanently
Server: nginx/1.25.1
Date: Tue, 19 Sep 2023 18:13:01 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: close
X-Powered-By: PHP/8.2.10
Location: https://example.com
Benjamin Crozat

Written by Benjamin Crozat

Indie hacker, blogger, and AI enthusiast building things with the TALL stack. 🔥

Follow me on:

Recommended articles

7 Laravel RESTful APIs best practices for 2023 7 Laravel RESTful APIs best practices for 2023

Master the art of crafting RESTful APIs with Laravel thanks to these best practices.

Modified on Oct 10, 2023

How does Laravel work? A crystal clear explanation. How does Laravel work? A crystal clear explanation.

Discover my step by step and simple explanation of how Laravel makes your life easier.

Published on Oct 31, 2023

Mastering error handling in Laravel's HTTP client Mastering error handling in Laravel's HTTP client

Learn how to use Laravel's HTTP client for efficient error handling and exception throwing in different scenarios with ease.

Modified on Sep 19, 2023

20+ Laravel best practices, tips and tricks to use in 2023 20+ Laravel best practices, tips and tricks to use in 2023

Learning a framework can be overwhelming, but time and execution will make you a master. Here are some best practices to help you toward your goal.

Modified on Oct 17, 2023

9 testing best practices for Laravel in 2023 9 testing best practices for Laravel in 2023

Are you familiar with testing? Good. Here are a bunch of best practices to help you level up even more!

Modified on Oct 27, 2023

Gold sponsors New

  • Wire Elements
    Beautiful handcrafted Livewire components.
    Check site
Your business here

Partners

If you buy from one of my partners below, I will be compensated at no cost to you. These are services I use or used, and 100% stand behind.

  • Scalable and reliable VPS hosting.
    Bonus: $200 of free credits
    Check site
  • The Google Analytics alternative without compromise.
    Free trial: 30 days
    Bonus: $10 off your first invoice
    Check site
  • Flare
    Track PHP and JavaScript errors in one place.
    Free trial: 10 days
    Check site
  • Keep track of your Google rankings.
    Free trial: 7 days
    Promo code: WELCOME30
    Check site
- / -