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.
TIP: This guide describes how to create normal 301 (and 302) redirects from one URL to another and how to redirect apex domains to www subdomains. To automatically redirect all HTTP requests to HTTPS, follow the instructions in our guide forcing a TLS redirect instead. It describes an easier way to configure this.
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.
- Log in to the Fastly web interface.
- From the All services page, select the appropriate service. You can use the search box to search by ID, name, or domain.
- Click the Edit configuration button and then select the option to clone the active version. 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.
-
Click Create a new condition. The Create a new condition window appears.
- Fill out the Create a new 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
). Theresp.status
needs to match the response code generated in the response above.
- 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.
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
.
- Log in to the Fastly web interface.
- From the All services page, select the appropriate service. You can use the search box to search by ID, name, or domain.
- Click the Edit configuration button and then select the option to clone the active version. The Domains page appears.
- Click the Settings link. The Settings page appears.
-
In the Redirect traffic to www subdomains area, click Add apex redirect. The Domain and Status selection menus appear.
- From the Domains menu, select the domain, subdomain, or wildcard domain you want to redirect. You can select more than one.
- From the Status menu, select the HTTP status code to send when redirecting traffic from the domains you selected.
- Click Add.
- Repeat the steps for redirecting traffic to www subdomains for any other domains you want to redirect.
- 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:
- Navigate to the Redirect traffic from Apex to www subdomains area.
- Hover your cursor over the entry you want to edit, then click the pencil icon that appears.
- 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.
- 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:
- Navigate to the Redirect traffic from Apex to www subdomains area.
- Hover your cursor over the entry you want to edit, then click the trash can icon that appears.
- Click the Confirm and delete button.