LOG IN SIGN UP
Documentation

Generating HTTP redirects at the edge

When users request information from your origin servers, you may want to redirect them for various reasons. For example, you may want to redirect them to either a secure or non-secure version of a web page, or to pages that have been moved or updated since the last time they were requested.

You can send these redirects from the edge rather than having to go to origin by creating a synthetic response with the appropriate redirect status code and then creating a content rule with the proper Location header.

Create a new response and condition

To generate redirects at the edge, start by creating a new response with the appropriate status code and a new condition describing when the response can be applied.

  1. Log in to the Fastly web interface and click the Configure link.
  2. From the service menu, select the appropriate service.
  3. Click the Edit configuration button and then select Clone active. The service version page appears.
  4. Click the Content tab. The Content page appears.
  5. Click the Create response button. The Create a new synthetic response page appears.

    the Create a new synthetic response page

  6. Fill out the Create a new synthetic response fields as follows:

    • From the Status menu, select the HTTP status code that should be included in the header of the response (e.g., 301 Moved Permanently or 302 Moved Temporarily for redirections).
    • Leave the MIME Type field blank.
    • In the Description field, type a meaningful name for your response (e.g., Redirect to blog). This name is displayed in the Fastly web interface.
  7. Click the Create button to create the new response.

  8. On the Content page, click the Attach a condition link to the right of the new response you just created. The Create a new request condition window appears.

    the New Condition window

  9. Fill out the Create a new request condition fields as follows:

    • From the Type menu, select Request.
    • In the Name field, type a meaningful name for your condition (e.g., URL is /wordpress). This name is displayed in the Fastly web interface.
    • In the Apply if field, type the logical expression to execute in VCL to determine if the condition resolves as True or False (e.g., req.url ~ "^/wordpress").
    • Leave the Priority set to 10.
  10. Click the Save and apply to button to create the new request condition.

Create a new header and condition

Complete the creation of a synthetic redirect by creating a new header and condition that modifies that response by adding the location header based on the status code and the matching URL. This ensures the redirect only applies when both of those are true.

  1. On the Content page, click the Create header button. The Create a new header page appears.

    the Create a new header page

  2. Fill out the Create a new header fields as follows:

    • From the Type menu, select Response, and from the Action menu, select Set.
    • In the Destination field, type http.Location.
    • In the Source field, type the source location of the new content (e.g., "http://www.example.com/new-location/of/item").
    • Leave the Ignore if set and Priority fields at their default settings.
    • In the Description field, type a meaningful name for your header (e.g., Location for WordPress redirect).
  3. Click the Create button to create the new header.

  4. On the Content page, click the Attach a condition link to the right of the new header you just created. The Create a new request condition window appears.

    the Create a new request condition window

  5. Fill out the Create a new request condition fields as follows:

    • From the Type menu, select Response.
    • In the Name field, type a meaningful name for your condition (e.g., Set location for blog redirect).
    • In the Apply if field, type the logical expression to execute in VCL to determine if the condition resolves as true or false (e.g., req.url ~ "^/wordpress" && resp.status == 301). The resp.status needs to match the response code generated in the response above.
    • Leave the Priority set to 10.
  6. Click the Save and apply to button to create the new condition.

  7. Click the Activate button to deploy your configuration changes.


Back to Top