Skip to main content
CSS-Tricks
  • Articles
  • Videos
  • Almanac
  • Newsletter
  • Guides
  • DigitalOcean
  • DO Community
Search
Code Snippets → PHP → PHP Redirect

PHP Redirect

Avatar of Chris Coyier
Chris Coyier on Sep 29, 2009
<?php
  header( 'Location: http://www.yoursite.com/new_page.html' ) ;
?>
Psst! Create a DigitalOcean account and get $200 in free credit for cloud-based hosting and services.

Comments

  1. Steven Rossi
    Permalink to comment# September 29, 2009

    It’s important to remember that this has to be the first output in the document for it to work. For examples, see the PHP Manual.

    Reply
  2. Peter Dubrovski
    Permalink to comment# September 29, 2009

    You should use
    header(‘location:http//www.domain.dd’);
    exit;

    Reply
  3. Ayo
    Permalink to comment# October 25, 2010

    Wat about testin from a local server.

    Reply
  4. Anonymouschicken
    Permalink to comment# January 4, 2011
    function redirect($url) {
                echo "";
                echo "Redirecting in 3 seconds";
                echo "<a href='$url' rel="nofollow">Click here if it does not redirect you</a>";
            }
    
    
     redirect("gotohere.php");

    A simpler yet adjustable php function to redirect.

    Reply
  5. Waterman
    Permalink to comment# August 24, 2011

    Don forget that the header function should be the first thing typed in, or you will have to turn on output buffering in your ini file or call this function, ob_start()

    Reply
  6. Peter Dubrovski
    Permalink to comment# October 21, 2011

    quick and dirty redirect (also if headers allready sent)

    
    function forceRedirect($url = '/'){
        if(!headers_sent()) {
            header('HTTP/1.1 301 Moved Permanently');
            header('Location:'.$url);  
            header('Connection: close');
            exit;
        }
        else {
            echo 'location.replace('.$url.');';
        }
        exit;
    }
    

    just call forceRedirect(‘http://www.domain.dd’);

    Reply
  7. Gosha
    Permalink to comment# August 17, 2012

    And use “UTF-8 without BOM” instead of “UTF-8” as your document encoding. Otherwise some “unnecessary” bytes will be send before PHP-interpreter will meet “” construction.

    Reply
  8. Joseph ben
    Permalink to comment# February 22, 2013

    how about redirect all the links in one page …
    some give me a script ,plzz

    Reply
  9. Linus
    Permalink to comment# December 28, 2016

    This is the function that I’ve got in my library

    function relocate($url) {
        if ($url == -1) $url = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : '/';
        Header("Location: $url");
        die();
    }
    
    relocate('./'); // home
    
    Reply
  10. Akash
    Permalink to comment# April 5, 2021

    Thanks for sharing. Would like to share that we need to make sure there is no output sent before calling header function. Else it won’t work as expected.

    Reply

Leave a Reply Cancel reply

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

CSS-Tricks is powered by DigitalOcean.

Keep up to date on web dev

with our hand-crafted newsletter

DigitalOcean
  • DigitalOcean
  • DigitalOcean Community
  • About DigitalOcean
  • Legal
  • Free Credit Offer
CSS-Tricks
  • Email
  • Guest Writing
  • Book
  • Advertising
Follow
  • Mastodon
  • Twitter
  • Instagram
  • YouTube
  • CodePen
  • iTunes
  • RSS
Back to Top