Skip to main content
Version: Fegg 5.x

Application::redirect

Redirect to another URL and stop

Description​

public void Application::redirect( string $url )

Send a Location header (HTTP 302) and end the response by throwing Fegg\Http\Halt (see Ending a response). Code after redirect() does not run.

  • A URL starting with http:// or https:// is used as is.
  • Any other URL is a site path: Fegg adds the leading /, a trailing / (no extension, see URL) and the base path, and sends it without scheme or host, so the scheme of the page is kept (see Security).

Arguments​

NameData typeDefaultRemarks
$urlstringAbsolute URL, or a path inside the site

Example​

Controller
// Location: /news/
$this->redirect('/news/');

// Location: /news/ (slashes added)
$this->redirect('news');

// Location: /contact/result.html
$this->redirect('/contact/result.html');

// Location: /sub/news/ (site installed in /sub/)
$this->redirect('/news/');

// Location: https://www.example.org/
$this->redirect('https://www.example.org/');

Do not pass a URL taken from the request (for example $this->in('return_to')) without checking it, or the site becomes an open redirect.