How do I redirect requests for example.com to www.example.com (or the other way around)?
Short answer
With the Location Redirection action. First, an HTTP Header condition that selects the requests whose Host Header is example.com is created. Then, in the Location Redirection action, the target is written as https://www.example.com, and the path and query part of the request is added with Smart Variables (Path + query of the request); this condition is bound to the action, and the action is added to the vService's Actions list. Location Redirection does not work without a bound condition; in HTTPS, the certificate must cover both names.
Problem: Why Should the Same Site Be Gathered at a Single Address?
If a site opens under both the example.com and the www.example.com name, search engines see it as two separate sites, and cookies and analytics data are split in two. For this reason, one of the names is chosen as the main address, and requests arriving at the other are sent to it with a permanent (301) redirect. While redirecting, the path and query parameters of the requested page must be preserved; otherwise the user always lands on the home page.
Solution
Host Condition and Location Redirection Action
- How it works A request that matches the condition is redirected to the address written in the action without going to the Backend Service; the target address carries the path and query information of the request with Smart Variables.
- Where it is openedLoad Balancer → Conditions → HTTP Header; Load Balancer → Actions → Location Redirection; vService → Details → Actions
- Requirement In HTTPS, a certificate that covers both names; if Virtual Hosting is used, both names must be in the list; the DNS records of both names must point to the vService's IP
- vService type HTTP
Step-by-Step Configuration
The Load Balancer → Conditions → Add steps are followed.
HTTP Header is selected as the Criteria; the Header Name field comes filled with Host. Exact Match (default) is selected as the matching method, and example.com is written as the value (www.example.com for the other way around).
The Add button is clicked.
The Load Balancer → Actions → Add steps are followed; Location Redirection is selected as the Action Type. Since the action works only at the request stage, there is no Execution Level field in the form.
In the Redirect Code field, 301 (default) is selected for a permanent redirect. To avoid getting stuck in the browser cache during trials, it can first be tested with 302 and then switched to 301.
https://www.example.com is written in the Redirect Location field, and the Path + query of the request (↓PathQ) variable is added right after it with the fx (Smart Variables) button. The path and query are not carried automatically; if it is not added, the user is always redirected to the home page.
In the Conditions field, the condition saved in step 3 is selected. A Location Redirection with no bound condition redirects no requests.
The Add button is clicked.
The vService is opened on the Load Balancer → Load Balancer vService List screen; the action saved in step 8 is added to the Details → Actions list. If there are other redirect or blocking actions in the list, the order is checked; actions run from top to bottom.
The Save button is clicked.
Verification
curl -I "https://example.com/urunler?sayfa=2"HTTP/2 301 · location: https://www.example.com/urunler?sayfa=2curl -I https://www.example.com/HTTP/2 200Common Mistakes
- Not binding a condition. A Location Redirection without a condition redirects no requests.
- Matching with Includes. Searching for the
example.comvalue with Includes also matcheswww.example.com, and an infinite redirect loop occurs; Exact Match is used. - Not adding the path and query. The redirected user always lands on the home page; the parameters are lost.
- The certificate not covering both names. In an
https://example.comrequest, the TLS handshake takes place before the redirect; if the certificate does not coverexample.com, the browser shows a warning. - Forgetting to add the action to the vService. An action saved on the Actions screen does not work until it is added to the vService's Actions list and the vService is saved.
- Testing with 301. Browsers keep 301 in the cache; trials are made with 302, and after verification it is switched to 301.
How it is done in the documentation
