Maximize your Laravel blog's potential with SEO best practices and reach 10K monthly clicks on Google.
Preview the course for free
Benjamin Crozat New!
Benjamin Crozat The art of crafting web applications

PHP redirect: 301 and 302

Benjamin Crozat — Updated on
Artboard

Hundreds of developers subscribed to my newsletter.
Join them and enjoy free content about the art of crafting websites!

Powered by

To perform a 302 redirect in PHP, use the header() function to set the new location.

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

Easy, right? There’s one thing to remember going forward, though.

Table of contents

PHP redirect: 301 and 302

Don’t output text before sending headers

<?php
 
echo 'Hello, World!';
 
header('Location: https://example.com/some/page');

This will result in a warning:

Warning: Cannot modify header information - headers already sent by

Stop code execution

Most of the time, you will need to stop code execution with the exit() function. Here’s an example:

<?php
 
if (! empty($_POST['redirect'])) {
header('Location: https://example.com/some/page');
 
exit;
}
 
// The following code will never be executed thanks to exit().

If you don’t stop code execution, the user will be redirected to the new URL after the code has finished running.

Set the correct HTTP code

The header() function can take a third parameter.

For instance, when you add a Location header, the HTTP code will automatically be set to 302.

But what if we want to perform a 301 redirect to the new URL instead?

header('Location: https://example.com/some/page', true, 301);

Frequently Asked Questions

Should a redirect be 301 or 302?

Most of the time, you should stick to the default 302 redirect, unless you want to permanently redirect your users.

Is 302 a permanent redirect?

302 redirects are not permanent.

What are 302 redirects used for?

302 redirects are used for the following scenarii:

What are 301 redirects used for?

The HTTP 301 code means the resource has been moved permanently.

Some use cases include:

Which is better, a 301 or 302 redirect?

None is better than the other. Use whichever is the most appropriate to your needs. This article should help you figure it out.

Recommended

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.

Nailing a Laravel job interview can be a daunting task, but with the right preparation and mindset, you can set yourself up for success.

Here's a case study for my blog in the programming niche, where I share everything I did to increase clicks by a huge amount since the beginning.

Laravel 10 has been released on February 14, 2023. Let's dive into every relevant new feature and change.

Take your code to the next level, thanks to exceptions. Handle errors in a more graceful way within try and catch blocks.

I show you how to upgrade your Laravel 9 project to version 10 and help you decide whether the return on investment is worth it.

Start leveraging the power of AI today. It enables developers to do incredible things, and many startups build products around it.

switch, case, and break. What are all these? When should you use it instead of if? What are its pros and cons?

I show you how to upgrade your Laravel 8 project to version 9 and help you decide whether the return on investment is worth it.

There are multiple ways to check if an array is empty. Let me tell you about each of them and why and when you should use them.

Powered by