PHP redirect 301 – code example

HTTP 301 redirects are a popular method for taking website visitors and redirecting them to the homepage of another domain with the same or similar idea/content. This is not only more user-friendly but also helps increase search traffic and make sure that users don’t get confused. For more information on using HTTP 301 redirects, be sure to check out our post on the subject. How to Redirect Old Domain to New Domain Using htaccess Redirect. Where you can learn about how to properly create an HTTP 301 redirect rule in htaccess or PHP.

How to Make a Redirect in PHP

You can simply use the PHP header() function to redirect a user to a different page. To redirect 301 in PHP, you’ll first need to write your header() function, starting with header().

Then, within the parentheses, you’ll define the Location response-header field with the URL or file name you wish to redirect users and search engines to. Supported files include PHP, HTML, Python, CGI, Perl, or compiled CGI programs. So your header function might look something like this:

The following PHP code will redirect a user from a page to an alternate destination. A link to that destination can be placed on any web page and when clicked, the browser will navigate to the alternate URL.


After the semicolon, you’ll have to add one last function: either the die() or exit() function. Without either of these functions, search engine crawlers or bots can ignore the header function and continue processing the page you wanted to redirect away from.

Now let’s try an example of Permanent 301 redirect


//* Permanently redirect page
header("Location: new_page.php",TRUE,301);

Now let’s try an example of Simple 301 redirect


header("Location:", true, 301);

That’s all for this article if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn

Suggested Articles:

  1. How to Rewrite Query String to Path with htaccess

18 thoughts on “PHP redirect 301 – code example”

  1. It’s a pity you don’t have a donate button! I’d without a
    doubt donate to this outstanding blog! I guess for now I’ll settle for bookmarking and adding your RSS
    feed to my Google account. I look forward to fresh updates
    and will talk about this website with my Facebook group.
    Chat soon!

  2. Hey, are using WordPress for your blog platform? I’m new to the blog world but I’m trying to get started
    and set up my own. Do you need any coding expertise to make your
    own blog? Any help would be greatly appreciated!

  3. Woah! I’m really loving the template/theme of
    this blog. It’s simple, yet effective. A lot of times it’s difficult
    to get that “perfect balance” between usability and visual appeal.
    I must say that you’ve done a great job with this. Also, the blog loads very quick for me on Firefox.
    Superb Blog!

  4. Hey I am so excited I found your site, I really found
    you by error, while I was searching on Bing for something
    else, Nonetheless, I am here now and would just like to say
    kudos for a marvelous post and an all-round thrilling blog (I also love the

  5. It’s truly a great and useful piece of info.
    I am satisfied that you simply shared this useful info with us.
    Please keep us informed like this. Thanks for sharing.

  6. I’m extremely impressed with your writing skills as well as
    with the layout of your weblog. Is this a paid theme or did you customize it yourself?

    Anyway keep up the nice quality writing, it’s rare to see a great blog like this one these days.


Leave a Comment