Benjamin Crozat The web developer life

PHP redirect: 301 and 302

Benjamin Crozat Benjamin Crozat1 minute read

Before you start reading this article, did you know 76 persons subscribed to my newsletter?

Join them and enjoy free content about the art of crafting websites!

PHP redirect: 301 and 302

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

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 be able to help figure it out.

Other posts to read

Let's review some quick wins that Laravel Collections provide to instantly make your codebase better.

Hiring a good developer can be challenging. But if you follow these tips and you will be able to hire the right fit for your projects.

Learn why the "Invalid argument supplied for foreach()" warning happens, and let me show you multiple ways to fix 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.

AI is a trending topic in the programming space. It enables developers to do incredible things, and many startups build products around it.

Debugging requires dissecting everything. Here's a list of all the one-line of code built-in ways to print arrays in PHP.

You heard about Laravel but don't know where to start? Don't worry, you found the best starting point for your journey.

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

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.

When in doubt, clear the cache. In this article, you'll learn about how to clear every cache Laravel uses.