How to Make a Redirect in PHP?

How to Make a Redirect in PHP?

1 months ago | 2 Views | Post Reply

Imagine launching a new website where some page URLs have changed. Visitors trying to access the old URLs land on an error page, leading to a poor user experience. A simple redirect ensures they are automatically sent to the correct page, preserving traffic and improving usability.

PHP, a powerful server-side language, offers efficient ways to create redirects. Whether it’s moving from HTTP to HTTPS, redirecting from non-www to www, or updating a page URL, this guide explains how to implement them step-by-step.

What Is a PHP Header Redirect?

A PHP header redirect sends users or search engines from one URL to another automatically. It uses the header() function to specify the new location, making it ideal for handling old URLs or structuring website flows.

For instance, if your site moves from old-url.php to new-url.php, a PHP header redirect ensures users don’t encounter errors when accessing the old page.

Here’s a basic example:

<?php header("Location: https//:www.techthaastu.com"); exit(); ?>

The exit() ensures no further code is executed after the redirect.

What Are HTTP Status Response Codes?

HTTP status codes indicate the result of a client’s request to the server. For redirects, the most common codes are:

  1. 301 (Moved Permanently): Use this for permanent redirects when a page’s URL changes indefinitely.
  2. 302 (Found): Temporary redirects, often used for maintenance or testing.
  3. 307 (Temporary Redirect): Similar to 302 but ensures the same HTTP method (GET or POST) is used for the next request.

Here’s how to implement a 301 redirect in PHP:

<?php header("HTTP/1.1 301 Moved Permanently"); header("Location: https://www.example.com/new-page"); exit(); ?>

PHP Redirection Examples

Redirect to Another URL

Redirect users to a different page within the same domain or to an external site:

<?php header("Location: https://www.example.com"); exit(); ?>

HTTP to HTTPS Redirect

To enhance security, redirect all HTTP traffic to HTTPS:

<?php if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] !== 'on') { header("Location: https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); exit(); } ?>

Non-www to www Redirect

Standardize your website URL for better SEO:

<?php if (substr($_SERVER['HTTP_HOST'], 0, 4) !== 'www.') { header("Location: https://www." . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); exit(); } ?>

Best Practices When Setting Up Redirects

  1. Use the Correct HTTP Status Code
    Choose the appropriate status code (301 or 302) based on whether the change is permanent or temporary.

  2. Test Redirects
    Verify redirects using tools like Redirect Checker to avoid errors like redirect loops.

  3. Update Links
    Update internal and external links to reflect new URLs, even if redirects are in place.

  4. Minimize Redirect Chains
    Avoid multiple redirects (e.g., A → B → C). Directly link A → C for better performance.

Alternative Methods to Implement Redirects

Redirect Using JavaScript

For client-side redirects:

<script> window.location.href = "https://www.example.com"; </script>

Redirect Using jQuery

If you’re already using jQuery, the same result can be achieved with:

$(document).ready(function() { window.location.href = "https://www.example.com"; });

While these methods work, server-side PHP redirects are faster and more reliable.

Why Use Redirects?

Redirects serve multiple purposes:

  • Ensure a seamless user experience during website restructuring.
  • Prevent broken links after changing page URLs.
  • Enforce HTTPS or preferred domain structures.
  • Redirect users based on device type or location.

Conclusion

Redirects play a crucial role in managing website traffic and maintaining SEO. Learning how to make a redirect in PHP ensures visitors seamlessly navigate to the right pages, whether you’re updating URLs, moving from HTTP to HTTPS, or restructuring your site. By following best practices and choosing the right redirect type, you can enhance the user experience and preserve your site’s rankings.

Start implementing PHP redirects today to manage your website effectively and keep your audience engaged.

Author

Hemant Sharma Manager | Owner VBageTech
Hemant Sharma
Manager | Owner | IT

Proficient in a wide range of programming languages including Java, Kotlin, PHP, and Flutter, among others. Demonstrated ability to design, develop, and implement robust and scalable solutions for diverse projects.

Explore Me
Related Blogs

Some more related to Custom Website Development category

Firebase PHP Push Notification Send: A Complete Guide
Firebase PHP Push Notification Send: A Complete Guide

Discover how to implement Firebase PHP Push Notification Send using FCM HTTP v1 APIs. Learn step-by-step integration with examples for sending notifications.

Support Call
Support Whatsapp
Project_With_Us_graphic

Powering Digital Experiences For Globally-Admired Organizations

We help companies accelerate innovation and build a better future by putting digital at the forefront of business strategies