Jump to main content Jump to doc navigation

modX::sendRedirect

Sends a redirect to the specified URL using the specified method.

Syntax

void sendRedirect (string $url, [array|boolean $options = false], [string $type = ''], [string $responseCode = ''])

$url must be a valid URL — modX::makeUrl builds one. An empty URL is not redirected: the method writes an error to the log and returns without sending a header.

$type and $responseCode are deprecated since 2.0.5 in favour of the type and responseCode keys of $options. 3.x still accepts them, but each use writes a deprecation record to the log — pass the values in the $options array instead.

$options accepts an array (or a boolean, treated as the count_attempts flag) with one or more of the following key/value pairs:

  • type, one of the following (REDIRECT_HEADER is the default):
    • REDIRECT_REFRESH - Uses the header refresh method
    • REDIRECT_META - Sends a META HTTP-EQUIV=“Refresh” tag to the output
    • REDIRECT_HEADER - Uses the header location method
  • responseCode the full HTTP status line, not just 301 or 302. Defaults to HTTP/1.1 302 Moved Temporarily; pass HTTP/1.1 301 Moved Permanently for a permanent redirect.
  • count_attempts (boolean) appends an ?err=N counter to the URL to break redirect loops. After 3 attempts — the limit is hard-coded — the redirect is treated as fatal: a FATAL entry is logged and a fatal error page is sent.
  • preserve_request (boolean) when set, the current request is preserved (see modRequest::preserveRequest) before the redirect, so the referrer survives.

Examples

Redirect to the Resource with ID 54.

$url = $modx->makeUrl(54);
$modx->sendRedirect($url);

Send a redirect to modx.com via the META HTTP-EQUIV refresh tag.

$modx->sendRedirect('https://modx.com',array('type' => 'REDIRECT_META'));

Send the 301 Moved Permanently response code instead of the default 302 Moved Temporarily.

$modx->sendRedirect('https://modx.com',array('responseCode' => 'HTTP/1.1 301 Moved Permanently'));

See Also

Support the team building MODX with a monthly donation.

The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.

Backers

  • modmore
  • STERC
  • Digital Penguin
  • Jens Wittmann – Gestaltung & Entwicklung
  • CrewMark
  • Fabian Christen
  • Sepia River Studios
  • Dannevang Digital
  • A. Moreno
  • Alex
  • Chris Fickling
  • Stéphane Jäggi
  • Murray Wood
  • Anton Tarasov
  • JT Skaggs
  • deJaya
  • Lefthandmedia
  • eydolan
  • Following Sea
  • Guido Gallenkamp
  • YJ
  • Raffy
  • Snow Creative
  • Nick Clark
  • Guest
  • Helen
  • krisznet
  • Yanni
  • Richard

Budget

$194 per month—let's make that $500!

Learn more