Version 5 supported


Controllers can facilitate redirecting users from one place to another using HTTP redirection using the Location HTTP header.

namespace App\Control;

use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPResponse;

class MyController extends Controller
    // ...

    public function someMethod(): HTTPResponse
        // redirect to Page::goherenow(), i.e.on the contact-us page this will redirect to /contact-us/goherenow/
        return $this->redirect($this->Link('goherenow'));

        // redirect to the URL on assuming your website is hosted at
        // (note the leading slash)
        return $this->redirect('/goherenow');

        // redirect to (assuming that is an external website URL)
        return $this->redirect('');

        // go back to the previous page
        return $this->redirectBack();

Back URL

The BackURL get parameter is one mechanism the redirectBack() method uses to know where to redirect to. It also checks for a legacy X-Backurl header and a referer header, and failing that just redirects to the base URL for your website.

You can use the BackURL parameter if you want an action to redirect users to some specific path. You might want to do this for example to force unauthenticated users to log in before performing an action.

use SilverStripe\Control\Controller;
use SilverStripe\Security\Security;

$args = ['BackURL' => $this->Link('someAction')];
$this->redirect(Controller::join_links(Security::login_url(), '?' . http_build_query($args)));

If there's already a BackURL parameter in the current request's URL, you can add that directly to any link:

$linkWithBackURL = $this->addBackURLParam($this->Link('someAction'));

Status codes

The redirect() method takes an optional HTTP status code, either 301 for permanent redirects, or 302 for temporary redirects (default).

// go back to the homepage, don't cache that this page has moved
$this->redirect('/', 302);

Redirections in routing rules

You can define redirections in the director routing rules. There are two ways to declare redirections in routing rules.

If the routing rule pattern starts with ->, it will be interpreted as a redirect.

    'about': '->about-us'

You can also explicitly declare the rule as a redirection using the following more explicit syntax, which can be a useful way to better visually distinguish redirection routes from controller routes.

      Redirect: 'about-us'

The path to redirect to will be interpreted as being relative to your site root unless you explicitly set a protocol or prefix the URL with // like so:

    'absolute-url1': '->//'
    'absolute-url2': '->'

For more information about routing rules see the Routing documentation.

API documentation