Monday , February 3 2025

301 redirect in PHP

There are different reasons for different engines to add 301 redirects not in .htaccess. For DLE, PHP redirects are the only way to avoid looking at dynamic garbage generated in links.

Examples implementation in PHP:

if ( getenv('REQUEST_URI') == '/page-before-redirect/' )
{
header( "HTTP/1.1 301 Moved Permanently" );
header( "Location: http://old.domain.com/page-after-redirect/" );
exit;
}
if ($_SERVER['REQUEST_URI'] == "/page-before-redirect/") {
        header('HTTP/1.1 301 Moved Permanently');
        header("Location: http://old.domain.com/page-after-redirect/");
        die('redirect from page-before-redirect');
};

About iryna

I'm Iryna, a web developer from Ukraine with a decade of experience solving complex technical challenges in the world of freelance. Throughout my career, I've worked on everything from troubleshooting server-side issues and optimizing website performance to enhancing user interfaces. On this blog, I share detailed solutions to the technical problems I’ve encountered and methods that have worked best for me. In addition to my technical expertise, I’m also passionate about digital drawing. I hope the tutorials and insights I provide here will help both fellow developers and creatives alike in their own projects.

Check Also

301 redirect in .htaccess from uppercase to lowercase characters

In web development, managing URL consistency is crucial for SEO and user experience. One common …

Leave a Reply

Your email address will not be published. Required fields are marked *