PHP Redirect

<?php
  header( 'Location: http://www.yoursite.com/new_page.html' ) ;
?>

Comments

  1. User Avatar
    Steven Rossi
    Permalink to comment#

    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.

  2. User Avatar
    Peter Dubrovski
    Permalink to comment#

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

  3. User Avatar
    Ayo
    Permalink to comment#

    Wat about testin from a local server.

  4. User Avatar
    Anonymouschicken
    Permalink to comment#
    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.

  5. User Avatar
    Waterman
    Permalink to comment#

    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()

  6. User Avatar
    Peter Dubrovski
    Permalink to comment#

    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’);

  7. User Avatar
    Gosha
    Permalink to comment#

    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.

  8. User Avatar
    Joseph ben
    Permalink to comment#

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

  9. User Avatar
    Linus
    Permalink to comment#

    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
    

Submit a Comment

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

icon-anchoricon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag