If you run a business website, you may occasionally change how your site is structured—a change that requires a PHP redirect. You might reorganize pages to improve search visibility or create a more intuitive sitemap, but without proper redirects, your existing URLs can break.
PHP redirects help you make sure customers and search engines land on the right page instead of getting a 404 error. They allow your server to send your users to the correct URLs, whether that’s a different page on the site or a new page you’ve created. Below, learn about the different types of PHP redirects and how to create redirects to keep your site visitors.
What is PHP?
PHP is a server-side programming language used in web development to build dynamic websites and web apps. It runs on the web server, not the user’s browser, and can process application logic, interact with databases, and handle forms, sessions, and authentication. PHP typically outputs HTML, but it can also return other web formats when needed.
PHP code was created to make web pages respond to user input, enabling servers to:
-
Show different relevant content to different users
-
Send requests to and call data from databases
-
Handle logins
-
Process forms
-
Fix broken links
-
Control page flow (like redirects, permissions, and routing)
PHP is commonly used for content-driven sites like blogs or news sites, ecommerce sites, content management systems (CMS), and internal tools like dashboards. Well-known web platforms that are built with PHP include WordPress and Wikipedia’s MediaWiki.
What is a PHP redirect?
A PHP redirect is an instruction sent by a PHP file that tells the user’s browser to load a new location before any content from the original page is sent. It’s a clean, server-level instruction that requires no JavaScript and runs before HTML renders, which can help ensure your users are sent to the right page as soon as possible.
At a high level, a PHP redirect looks like this:
<?php
header("Location: /new-page");
exit;
This code tells the browser to load a different URL before any page content is sent.
The HTTP Location header tells the user agent (the browser) where the new URL lives, and the browser never sees the original page or its output. The HTTP response also includes a status code—like a 301 for a permanently moved page or a 302 for a temporary move—sent via PHP’s header function. This helps search engines understand whether to update their index or keep the original URL, which directly affects whether your page retains its search ranking.
Because a PHP redirect is a server-side redirect, it’s generally more reliable than JavaScript redirects or meta refresh techniques. Those client-side approaches rely on the browser to handle the redirect after the page starts loading, which can cause layout flashes, delay rendering, and confuse search engine crawlers.
Common reasons you might want to use a PHP redirect include:
-
Sending users to a login page when they’re not already authenticated
-
Moving traffic after a change to a specific URL (like when you clean up for SEO purposes)
-
Routing users based on role, language, or device size
-
Handling legacy URLs that you don’t want to delete
Types of PHP redirects
There are several types of PHP redirect, each with a specific role to play in sending your client browsers to the correct resource using HTTP status codes. These include:
-
301 permanent redirect. This status code tells browsers and search engines that the resource has been permanently moved to a new URL. It’s the preferred redirect method for SEO because it passes link equity to the new location.
-
302 temporary redirect. This status code indicates that the resource is temporarily located at a different URL, and the original URL should still be used for future requests.
-
307 temporary redirect (strict). Similar to a 302 status code, a 307 redirect requires that the browser use the same HTTP request method as before. This can get a bit technical, but think about an online checkout system that relies on a POST request to gather payment information; you want that same request to continue at the temporary URL, or the checkout system won’t work.
Beyond status codes, you can also apply conditional redirects based on specific user states:
-
Authentication-based redirect. This lets you redirect users to a login page if they’re not logged in and are trying to access a protected page.
-
Role-based redirect. This type of redirect sends users to an admin-only page if they have the right permissions.
-
Device- or locale-based redirect. Use this type of redirect to route users based on the type of device they’re using or their location. Device detection for such redirects is often performed by examining the “user agent” string provided by the browser, which allows you to identify mobile or desktop devices and apply device-specific routing.
How to create a PHP header redirect
- Identify where the redirect code must run
- Send a redirect using PHP headers
- Stop script execution
- Add status codes (optional)
PHP redirects work by sending HTTP headers from the web server to the browser, which means your users won’t see any HTML, white space, or flickering when they load the original URL. Here’s how to set one up:
1. Identify where the redirect code must run
Place the PHP redirect code at the very top of your PHP file, before any output is sent to the browser. This ensures the header call works correctly and prevents issues.
If there’s any output before the header call— even a single space or blank line of white space or HTML—PHP will throw a “headers already sent” error message, and the redirect won’t process. This is one of the most common PHP redirect issues, and it’s easy to miss.
2. Send a redirect using PHP headers
The header function sends an HTTP location header to the browser, instructing it to load a new URL. It replaces the current page in the browser—think of it as telling the server, “Hey, stop loading this page and do this instead.”
3. Stop script execution
The “exit” line in your redirect code stops the script immediately, so the web server doesn’t continue loading the rest of the old URL’s HTML code. Without it, PHP may continue executing logic below the redirect, which can cause unexpected behavior.
4. Add status codes (optional)
You’ll most likely want to specify the HTTP status code so browsers and search engines handle the redirect correctly. If you don’t specify one, PHP defaults to 302—which could hurt your SEO if the move is actually permanent.
Using a 301 redirect looks like this:
301 – Permanent
< ?php
header(“Location: /new-page.php”, true, 301);
exit();
?>
A 302 redirect looks like this:
302 – Temporary
< ?php
header(“Location: /new-page.php”, true, 302);
exit();
?>
A 307 redirect looks like this:
307 - Temporary, use the same METHOD
< ?php header(“Location: http://www.example.com/new-location“, true, 307);
exit;
?>
The key difference across all three is the status code number. Everything else in the syntax stays the same.
When specifying the redirect target, use absolute URLs to avoid redirect issues and ensure site stability. This means the URL includes the full path—like https://www.shopify.com rather than shopify.com. Relative URLs can cause redirect chains or loops, where the browser bounces between URLs without ever landing on the right page.
How to create redirects with Shopify
If you’re using Shopify, you can set up redirects without PHP or coding, since it can all be done through the admin panel:
1. From your Shopify admin, go to Content > Menus.
2. Click “View URL Redirects.”
3. Click “Create URL redirect.”
4. In “Redirect from,” enter the old URL that you want to redirect visitors from.
5. In “Redirect to,” enter the new URL that you want to redirect visitors to. To redirect to your store’s home page, enter /.
6. Click “Save redirect.”
Shopify will only let you redirect from URLs that no longer serve a page (they’ve been deleted or unpublished), and some system URLs cannot be redirected to keep things from breaking. If you have a lot of redirects to add, you can bulk import them via a comma-separated value data set (CSV) as well.
PHP redirect FAQ
What is a redirect in PHP?
A PHP redirect sends an HTTP header that tells the browser to load a different URL before any page content is shown.
What is the purpose of a redirect?
Redirects route users to the correct page, handle URL changes, protect restricted pages, and prevent errors like duplicate form submissions.
How do you redirect a URL in PHP?
Use the header("Location: /new-url", true, 301 or 302); function before any output, then immediately stop the script with exit;.





