Redirect - Liquid Tag Reference
On this page
The redirect simple tag sends the browser to a different URL and stops rendering. Use it in a Liquid controller to redirect users after validation, form submission, authentication checks, or conditional logic.
:::note
This tag works only inside a Liquid controller template (at controllers/<controller>/<action>.liquid). A redirect in before skips the standard action and subsequent rendering.
:::
When redirect runs
Use redirect to send the user to a new URL:
```liquid
{% before %} {% if current_customer == blank %} {% redirect to: current_store.account_login_path %} {% endif %} {% endbefore %} ```
When redirect is called, the browser receives a redirect response and rendering stops.
Syntax
```liquid
{% redirect to: target_path, notice: “Success message” %} ```
| Property | Value |
|---|---|
| Tag Name | redirect |
| Type | Simple tag |
| Source | StoreConnect |
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
to |
String | Yes | The target path or URL. Use current_store.<path> properties or relative paths, never hardcoded store URLs. |
notice |
String | No | A flash notice message displayed on the target page. |
alert |
String | No | A flash alert message displayed on the target page. |
status |
Integer | No | HTTP status code (default: 302). Use 301 for permanent redirects, 303 for POST-to-GET. |
data |
Hash | No | Additional data to pass with the redirect (implementation-specific). |
What’s available in redirect
current_request.params— still accessible- All globals —
current_store,current_customer,current_cart, etc. - Store paths —
current_store.home_path,current_store.account_path,current_store.cart_path,current_store.orders_path, etc. - Relative paths — local paths starting with
/
Common tasks with redirect
1. Require authentication before accessing a page
```liquid
{% before %} {% if current_customer == blank %} {% redirect to: current_store.account_login_path %} {% endif %} {% endbefore %} ```
2. Redirect with a flash message after successful action
```liquid
{% before %} {% action “account.update_email”, email: current_request.params.email %} {% endbefore %}
{% after %} {% redirect to: current_store.account_path, notice: “Email updated successfully” %} {% endafter %} ```
3. Validate input and redirect on error
```liquid
{% before %} {% assign order_id = current_request.params.id %}
{% if order_id == blank %} {% redirect to: current_store.orders_path, alert: “Order ID is required” %} {% endif %}
{% params order_id: order_id %} {% endbefore %} ```
Key behaviors
- Stops rendering immediately — the template is not rendered after a redirect
- Redirect in
beforeskips standard action — if you redirect in thebeforeblock, the standard controller action and all subsequent rendering is skipped - Flash messages appear on target page —
noticeandalertare set in the session and displayed by the target page’s rendering logic - Use Store paths, not hardcoded URLs — always use
current_store.home_pathinstead ofhttps://example.com/to ensure portability - Relative paths are allowed —
/account,/products, etc. redirect to local paths - Default status is 302 — a temporary redirect; use
status: 301for permanent redirects
Best practices
Always use current_store paths
```liquid
{% before %} {% if current_cart.items.size == 0 %} {% redirect to: current_store.products_path %} {% endif %} {% endbefore %} ```
Build messages for the user
```liquid
{% after %} {% if current_cart.items.size > 10 %} {% redirect to: current_store.cart_path, alert: “Your cart has more than 10 items. Please review.” %} {% else %} {% redirect to: current_store.checkout_path, notice: “Ready to check out?” %} {% endif %} {% endafter %} ```
Validate all parameters before acting
```liquid
{% before %} {% assign promo_code = current_request.params.code %}
{% if promo_code == blank %} {% redirect to: current_store.cart_path, alert: “Promo code is required” %} {% endif %}
{% action “promotion.apply”, code: promo_code %}
{% params promo_code: promo_code %} {% endbefore %} ```
Redirect in different phases
Redirect in before
```liquid
{% before %} {% if validation_fails %} {% redirect to: current_store.account_path, alert: “Invalid input” %} {% endif %} {% endbefore %} ```
In before, a redirect skips the standard action and all rendering.
Redirect in after
```liquid
{% after %} {% redirect to: current_store.thank_you_path, notice: “Order submitted” %} {% endafter %} ```
In after, the page has rendered, but rendering is replaced with the redirect.
Status codes
| Code | Meaning | Use case |
|---|---|---|
| 301 | Moved Permanently | The resource has moved forever; search engines update their index |
| 302 | Found (default) | Temporary redirect; search engines keep the original URL |
| 303 | See Other | POST-to-GET redirect (safest after form submission) |
| 307 | Temporary Redirect | Like 302, but preserves the HTTP method |
Related phases
{% before %}— use redirect to validate and guard access{% after %}— use redirect after rendering to send the user to a success page{% final %}— redirect does not work in final (response already sent)
Difference from respond
{% redirect %}— sends a redirect to a new URL (302 or custom status){% respond %}— sends a custom response body (JSON, HTML, etc.)- Use case — redirect to send the user to a new page; respond to send custom content
Was this article helpful?
Thanks for your feedback! It helps us improve our docs.