Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Summary of existing answers plus my own two cents:</p> <h2>1. Basic answer</h2> <p>You can use the <code>header()</code> function to send a new HTTP header, but this must be sent to the browser before any HTML or text (so before the <code>&lt;!DOCTYPE ...&gt;</code> declaration, for example).</p> <pre><code>header('Location: '.$newURL); </code></pre> <h2>2. Important details</h2> <p><strong>die()</strong> or <strong>exit()</strong></p> <pre><code>header("Location: http://example.com/myOtherPage.php"); die(); </code></pre> <p>Why you should use <code>die()</code> or <code>exit()</code>: <a href="http://thedailywtf.com/Articles/WellIntentioned-Destruction.aspx" rel="noreferrer">The Daily WTF</a></p> <p><strong>Absolute or relative URL</strong></p> <p>Since June 2014 both absolute and relative URLs can be used. See <a href="https://greenbytes.de/tech/webdav/rfc7231.html#header.location" rel="noreferrer">RFC 7231</a> which had replaced the old <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30" rel="noreferrer">RFC 2616</a>, where only absolute URLs were allowed.</p> <p><strong>Status Codes</strong></p> <p>PHP's "Location"-header still uses the <a href="http://en.wikipedia.org/wiki/HTTP_302" rel="noreferrer">HTTP 302</a>-redirect code, but this is not the one you should use. You should consider either <a href="http://en.wikipedia.org/wiki/HTTP_301" rel="noreferrer">301</a> (permanent redirect) or <a href="http://en.wikipedia.org/wiki/HTTP_303" rel="noreferrer">303</a> (other).</p> <p>Note: <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4" rel="noreferrer">W3C mentions</a> that the 303-header is incompatible with "many pre-HTTP/1.1 user agents. Currently used browsers are all HTTP/1.1 user agents. This is not true for many other user agents like spiders and robots.</p> <h2>3. Documentation</h2> <p>HTTP Headers and the <code>header()</code> function in PHP</p> <ul> <li><a href="http://www.php.net/manual/en/function.header.php" rel="noreferrer">What the PHP manual says</a></li> <li><a href="http://en.wikipedia.org/wiki/List_of_HTTP_headers#Responses" rel="noreferrer">What Wikipedia says</a></li> <li><a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30" rel="noreferrer">What the W3C says</a></li> </ul> <h2>4. Alternatives</h2> <p>You may use the alternative method of <code>http_redirect($url);</code> which needs the <a href="http://pecl.php.net/package/pecl_http" rel="noreferrer">PECL package pecl</a> to be installed.</p> <h2>5. Helper Functions</h2> <p>This function doesn't incorporate the 303 status code:</p> <pre><code>function Redirect($url, $permanent = false) { header('Location: ' . $url, true, $permanent ? 301 : 302); exit(); } Redirect('http://example.com/', false); </code></pre> <p>This is more flexible:</p> <pre><code>function redirect($url, $statusCode = 303) { header('Location: ' . $url, true, $statusCode); die(); } </code></pre> <h2>6. Workaround</h2> <p>As mentioned <code>header()</code> redirects only work before anything is written out. They usually fail if <a href="https://stackoverflow.com/questions/8028957/warning-headers-already-sent-in-php">invoked inmidst HTML</a> output. Then you might use a HTML header workaround (not very professional!) like:</p> <pre><code> &lt;meta http-equiv="refresh" content="0;url=finalpage.html"&gt; </code></pre> <p>Or a JavaScript redirect even.</p> <pre><code>window.location.replace("http://example.com/"); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload