Code Snippet

Home » Code Snippets » PHP » PHP Redirect

PHP Redirect

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

Subscribe to The Thread

  1. 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. Peter Dubrovski

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

  3. Wat about testin from a local server.

  4. Anonymouschicken
    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. Waterman

    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. Peter Dubrovski

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

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~