Skip to content

Controller

redirectTo()

redirectTo() — returns void

Available in: controller Category: Miscellaneous Functions

Redirects the browser to the supplied controller/action/key, route or back to the referring page. Internally, this function uses the URLFor function to build the link and the cflocation tag to perform the redirect.

NameTypeRequiredDefaultDescription
backbooleannofalseSet to true to redirect back to the referring page.
addTokenbooleannofalseSee documentation for your CFML engine’s implementation of cflocation.
statusCodenumericno302See documentation for your CFML engine’s implementation of cflocation.
routestringno—Name of a route that you have configured in config/routes.cfm.
methodstringno—HTTP method constraint used when matching routes.
controllerstringno—Name of the controller to include in the URL.
actionstringno—Name of the action to include in the URL.
keyanyno—Key(s) to include in the URL.
paramsstringno—Any additional parameters to be set in the query string (example: wheels=cool&x=y). Please note that Wheels uses the & and = characters to split the parameters and encode them properly for you. However, if you need to pass in & or = as part of the value, then you need to encode them (and only them), example: a=cats%26dogs%3Dtrouble!&b=1.
anchorstringno—Sets an anchor name to be appended to the path.
onlyPathbooleannotrueIf true, returns only the relative URL (no protocol, host name or port).
hoststringno—Set this to override the current host.
protocolstringno—Set this to override the current protocol.
portnumericno0Set this to override the current port number.
urlstringno—Redirect to an external URL.
delaybooleannofalseSet to true to delay the redirection until after the rest of your action code has executed.
encodebooleannotrueEncode URL parameters using EncodeForURL(). Please note that this does not make the string safe for placement in HTML attributes, for that you need to wrap the result in EncodeForHtmlAttribute() or use linkTo(), startFormTag() etc instead.
// 1. Redirect to an action after successfully saving a user.
if (user.save()) {
	redirectTo(action="saveSuccessful");
}

// 2. Redirect to a different controller and action on a secure server with extra query params.
redirectTo(controller="checkout", action="start", params="type=express", protocol="https");

// 3. Redirect to a named route and pass in a dynamic route variable.
redirectTo(route="profile", screenName="Joe");

// 4. Redirect back to the referring page (e.g. after a cancelled edit).
redirectTo(back=true);

// 5. Redirect to an external URL (requires allowExternalRedirects=true in config).
redirectTo(url="https://example.com/landing");

// 6. Delay the redirect until after the rest of the action has run, and set a flash message.
redirectTo(action="index", delay=true, flashMessage="Record deleted successfully.");

// 7. Use a 301 permanent redirect when moving a page.
redirectTo(controller="posts", action="index", statusCode=301);