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 pages that have been moved or updated since the last time they were requested. Or you may want to redirect their requests at the apex domain (e.g., example.com) level to a www subdomain (e.g., www.example.com) instead. You can send these redirects from the edge rather than having to go to origin. Redirecting traffic from one URL to another can be done by creating a synthetic response with the appropriate redirect status code and then creating a content rule with the proper Location header. Redirecting all traffic can be done by changing a single setting.

Redirect traffic from one URL to another

Use these steps to redirect traffic from one URL to another. For example, to redirect a request for a page that has been moved since the last time it was requested. Begin by creating a synthetic response with the appropriate redirect status code. Then, create 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.
  2. From the All services page, select the appropriate service. You can use the search box to search by ID, name, or domain.
  3. Click the Edit configuration button and then select the option to clone the active version. The Domains page appears.
  4. Click the Content link. The Content page appears.
  5. Click the Set up advanced response button. The Create a synthetic response page appears.

    the Create a synthetic response page

  6. Fill out the Create a synthetic response fields as follows:
    • In the Name field, enter a meaningful name for your response (e.g., Redirect to blog). This name is displayed in the Fastly web interface.
    • 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.
  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 the type of condition you want to create (e.g., Request).
    • In the Name field, enter 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, enter the logical expression to execute in VCL to determine if the condition resolves as True or False (e.g., req.url ~ "^/wordpress").
  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 header page appears.

    the Create a new header page

  2. Fill out the Create a header fields as follows:
    • In the Name field, enter a meaningful name for your header (e.g., Location for WordPress redirect).
    • From the Type menu, select Response, and from the Action menu, select Set.
    • In the Destination field, enter http.Location.
    • In the Source field, enter 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.
  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.
  5. Click Create a new condition. The Create a new condition window appears.

    the Create a new condition window

  6. Fill out the Create a new condition fields as follows:
    • From the Type menu, select Response.
    • In the Name field, enter a meaningful name for your condition (e.g., Set location for blog redirect).
    • In the Apply if field, enter 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.
  7. Click the Save and apply to button to create the new condition.
  8. Click the Activate button to deploy your configuration changes.

Redirect traffic to www subdomains

Use the Redirect traffic to www subdomains setting to redirect traffic for apex domains, subdomains, or wildcard domains to a www subdomain so that you always arrive in a consistent location. For example, you could redirect requests from example.com to www.example.com.

  1. If you haven't already done so, create a DNS record for the apex domain, subdomain, or wildcard domain you want to redirect. See our guide on apex domains for more information.
  2. Log in to the Fastly web interface.
  3. From the All services page, select the appropriate service. You can use the search box to search by ID, name, or domain.
  4. Click the Edit configuration button and then select the option to clone the active version. The Domains page appears.
  5. If you haven't already done so, add the apex domain, subdomain, or wildcard domain that you want to redirect. See our instructions on adding additional domains to your service for more information.
  6. Click the Settings link. The Settings page appears.
  7. In the Redirect traffic to www subdomains area, click Add apex redirect. The Domain and Status selection menus appear.

    the Redirect traffic to www subdomains area

  8. From the Domains menu, select the domain, subdomain, or wildcard domain you want to redirect. You can select more than one.
  9. From the Status menu, select the HTTP status code to send when redirecting traffic from the domains you selected.
  10. Click Add.
  11. Repeat the steps for redirecting traffic to www subdomains for any other domains you want to redirect.
  12. Click the Activate button to deploy your configuration changes.

Editing a www redirect

You can edit redirects at any time by following the steps below:

  1. Navigate to the Redirect traffic from Apex to www subdomains area.
  2. Hover your cursor over the entry you want to edit, then click the pencil icon that appears.
  3. Edit the redirect by doing one of the following:
    • to add another domain to the redirect, select it from the Domain menu selections.
    • to remove an existing domain from the redirect, click the X icon to the left of the domain name in the Domain menu.
    • to change the status of the redirect for the selected domains, select a new one from the Status menu.
  4. Click the Save button. The changes you make will be immediately applied to your configuration. If you can't save edits to your redirect, make sure at least one domain appears in the Domains menu. You can't delete a www redirect by deleting all the listed domains.

Deleting a www redirect

You can delete redirects at any time by following the steps below:

  1. Navigate to the Redirect traffic from Apex to www subdomains area.
  2. Hover your cursor over the entry you want to edit, then click the trash can icon that appears.
  3. Click the Confirm and delete button.

Using custom VCL

You can also use custom VCL to redirect traffic from one URL to another. For more information, see our redirects tutorial.

Back to Top