Generating HTTP redirects at the edge
Last updated September 11, 2019
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. 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.
TIP: This guide describes how to create normal 301 (and 302) redirects from one URL to another. If you are interested in automatically redirecting all HTTP requests to HTTPS, our guide to forcing a TLS redirect describes an easier way to do this.
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.
- Log in to the Fastly web interface and click the Configure link.
- From the service menu, select the appropriate service.
- Click the Edit configuration button and then select Clone active. The Domains page appears.
- Click the Content link. The Content page appears.
-
Click the Set up advanced response button. The Create a synthetic response page appears.
- Fill out the Create a synthetic response fields as follows:
- In the Name field, type 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
or302 Moved Temporarily
for redirections). - Leave the MIME Type field blank.
- In the Name field, type a meaningful name for your response (e.g.,
- Click the Create button to create the new response.
-
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.
- 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, 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"
).
- From the Type menu, select the type of condition you want to create (e.g.,
- 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.
-
On the Content page, click the Create header button. The Create a header page appears.
- Fill out the Create a header fields as follows:
- In the Name field, type 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, 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 Name field, type a meaningful name for your header (e.g.,
- Click the Create button to create the new header.
-
On the Content page, click the Attach a condition link to the right of the new header you just created. The Create a new response condition window appears.
- Fill out the Create a new response condition fields as follows:
- 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
). Theresp.status
needs to match the response code generated in the response above.
- In the Name field, type a meaningful name for your condition (e.g.,
- Click the Save and apply to button to create the new condition.
- Click the Activate button to deploy your configuration changes.
NOTE: These responses use a custom status number >500. They will appear as errors on the Real-time stats page even though they are desired behavior.