{% redirect %}
Redirects a "302 Found" HTTP response pointing to the location of the target. This is slower than using built-in redirects but is useful when other variables, processing, or conditionals need to be evaluated first.
Methods that control redirects, headers, status codes, and CORS.
Redirects a "302 Found" HTTP response pointing to the location of the target. This is slower than using built-in redirects but is useful when other variables, processing, or conditionals need to be evaluated first.
Simple Use Case: Basic redirect
{%- redirect '/thank-you' -%}Redirects the user to the given path or URL. The tag stops further rendering; use conditions (if/unless) when the redirect should be conditional, or Marketpath CMS redirects when the redirect is unconditional since they will perform better and do not require placeholder articles.
Redirect to a page when permission is missing
{%- if page.restrictiontype.is_valid and page.restrictionpage.is_valid -%}
{%- var restrictiontype = page.restrictiontype.value -%}
{%- unless permissions[restrictiontype].allowed -%}
{%- redirect page.restrictionpage -%}
{%- endunless -%}
{%- endif -%}Redirect to a restriction page when the user lacks the required permission.
Redirect to a url with a constructed path (e.g. login with return URL)
{%- if user.guest -%}
{%- redirect request.path | url_encode | prepend: "/log-in?r=" -%}
{%- endif -%}Build a login URL that includes the current path as a return parameter.
Sets one or more headers in the HTTP response.
There are a number of reserved and invalid headers that may not be set using this method - mostly including headers with predefined meanings such as HTTP connection headers, CORS headers, and content and security headers. If you attempt to set an invalid or disallowed HTTP response header, it will simply be ignored rather than outputting an error message.
Set a header to refresh
{%- var customHeader = "do refresh in 5" -%}
{%- set_header Refresh:"5; url=https://www.example.com" X-Custom-Header:customHeader -%}This example demonstrates the use of the set_header method to set 2 headers to refresh the page in 5 seconds and include a custom header with a value.
Set dynamic header by reference variable
{%- var fieldname = 'Username' -%}
{%- var headername = fieldname | prepend: 'X-Custom-' -%}
{%- set_header &headername:session[fieldname] -%}This example dynamically defines a header name based on the value of a fieldname variable, and then sets the header to the value of the matching session variable.
Sets the Content-Type header for the HTTP response.
{% set_content_type "application/xml" %}application/xml
{%- var contenttype = "text/csv" -%}
{%- set_content_type contenttype -%}text/csv
{%- var contenttype = "csv" -%}
{%- set_content_type "text/" | append: contenttype -%}text/csv
Sets the HTTP response status code.
{%- var objectType = "teapot" -%}
{%- set_response_code 418 objectType | prepend: "I'm a " -%}Set HTTP 404 (Not Found)
{% set_response_code 404 %}Set HTTP 418 (I'm a teapot) with message
{%- var objectType = "teapot" -%}
{%- set_response_code 418 objectType | prepend: "I'm a " -%}Enables CORS headers on the response.
If the "always" keyword is specified, CORS will be enabled for all origins. Otherwise it will only be enabled for the current origin (ie: {{ request.headers.origin }}).
From all origins
{% allow_cors always %}Allow requests from any origin.
From a specific domain
{%- if request.headers.origin == 'https://specifically-allowed-domain.com' -%}
{%- allow_cors -%}
{%- endif -%}Allow CORS only when the request Origin header matches a specific domain.
Based on custom logic
{%- set is_authorized = false -%}
{%- comment %}custom logic for authorizing the current request{% endcomment -%}
{%- if is_authorized -%}
{%- allow_cors -%}
{%- endif -%}Allow CORS only when your authorization logic permits.