How do I set up a redirect that adds or removes www?
Short answer
An HTTP Header condition that looks at the Host header (Header Name Host, Exact Match, Case Insensitive) is created; in the Location Redirection action, the target domain name and the Path + query of the request variable are written as the Redirect Location, and the action is added to the Actions field of the vService. To add www, the condition is example.com and the target is https://www.example.com; to remove www, the condition is www.example.com and the target is https://example.com. Both names must point to the IP of the vService in DNS, and the certificate in the Certificate field of the vService must cover both names.
Problem: The Site Opens with Two Different Names
When the site opens with both the example.com and the www.example.com name, search engines see two separate sites; link value, cookies and analytics are split between the two names. A single canonical name is selected, and requests coming to the other name are redirected to the canonical name with 301, with the same path and query.
Solution: Four Methods
The redirection is done only on an HTTP vService. First, it is verified that the DNS records of both names point to the IP of the vService and that, for HTTPS, the certificate covers both names; an HTTPS request has to complete the TLS handshake before it receives the redirection response.
| Your situation | Suitable method |
|---|---|
| Adding or removing www for a single domain name | Method 1: Host condition + Location redirection |
| Removing www on many domain names with a single rule | Method 2: General rule with Replace (first) |
| The non-canonical name will be handled on a separate vService | Method 3: Separate vService + Always |
| From the command line | Method 4: WEB-CLI |
Method 1: Host Condition + Location Redirection Recommended
- How it works A request coming with the non-canonical name is redirected to the canonical name with 301, with the same path and query; requests coming to the canonical name are not affected.
- Where it is openedLoad Balancer → Conditions · Load Balancer → Actions · vService → Details → Actions
- Requirement Both names point to the IP of the vService in DNS; for HTTPS, the certificate covers both names
- vService type HTTP
Step-by-Step Configuration
Load Balancer → Conditions → Add is clicked (how to add).
Criteria = HTTP Header; in the selector to the left of the Header Name field, Request and, as the name, Host are left as they are (the form opens with these values); matching Exact match, Case Insensitive checked. As the value, example.com is entered to add www, and www.example.com to remove www.
Add is clicked.
Load Balancer → Actions → Add is clicked (how to add).
Action Name is entered, and Action Type = Location Redirection is selected.
Redirect Code = 301: Moved Permanently (default). https://www.example.com (https://example.com to remove www) is written in the Redirect Location field, and the Path + query of the request variable is added right after it with fx; no / is written in between, since the variable starts the path with /.
The condition from step 2 is selected in the Conditions field.
Add is clicked.
On the Load Balancer → Load Balancer vService List screen, the HTTP vService that handles both names is selected and Edit is clicked.
Save is clicked. The change is applied without dropping open connections; in a cluster, it is also sent to the peer device with the default auto-sync (if Manual Sync Mode is on, with Synchronize).
Since the target is written directly with https://, the http://example.com/sayfa request goes to the https://www.example.com/sayfa address with a single response. Location redirection is evaluated before the HTTP → HTTPS redirection in the frontend row; the client does not see a two-step chain that goes first to https://example.com and then to www.
Verification
curl -sI "http://example.com/sayfa?x=1" and curl -sI "https://example.com/sayfa?x=1"
301 + Location: https://www.example.com/sayfa?x=1curl -sI https://www.example.com/sayfa
Response of the Backend Service (no redirection)Method 2: General Rule with Replace (first)
- How it works The Host value is taken as a Smart Variable, and the leading
www.is deleted with the Replace (first) function; all the domain names on the same vService are redirected to their names without www with a single action. - Where it is openedLocation Redirection → Redirect Location → fx
- Requirement All the names without www are in DNS and in the certificate
- vService type HTTP
Step-by-Step Configuration
Load Balancer → Conditions → Add is clicked.
Criteria = HTTP Header, Header Name = Request Host, the matching Start and the value www. are entered. Case insensitive is not checked: the ^www\. pattern in the function matches lowercase letters; if the condition also catches a name starting with WWW., the request is redirected to the same name without the address changing, and a loop occurs. Browsers always send the domain name in lowercase letters.
Add is clicked.
Load Balancer → Actions → Add is clicked.
Action Type = Location Redirection and Redirect Code = 301: Moved Permanently are selected.
https:// is written in the Redirect Location field; after it, the Request HTTP header content variable is added with fx, and host is entered as the header name.
In the variable window, Add Functions → Replace (first) is selected: To Replace = ^www\., New Value is left empty (the matching part is deleted). The \ before the dot is mandatory; an unescaped . matches any character.
In the same field, Path + query of the request is added after it with fx.
The condition from step 2 is selected in the Conditions field.
Add is clicked.
The vService is opened with Edit.
The action is selected in the Details → Actions field.
Save is clicked.
The result of the pattern for example requests:
| Host | Request | Location |
|---|---|---|
www.example.com | /sayfa?x=1 | https://example.com/sayfa?x=1 |
www.example.org | / | https://example.org/ |
www.shop.example.com.tr | /urun/5 | https://shop.example.com.tr/urun/5 |
example.com, api.example.com | - | The condition does not match; it is not redirected |
www.example.com:8443 | /a | https://example.com:8443/a (on a non-standard port, Host also carries the port, and it passes to the target as is) |
This rule is only for removing www. Since a general rule for adding www would also catch other subdomains such as api., Method 1 is used for each name.
Verification
Each domain name is tried in the form curl -sI "https://www.example.org/a?b=1".
301 + Location: https://example.org/a?b=1Method 3: Separate vService + Always
- How it works The non-canonical name is moved to a separate vService that shares the same IP and port through Virtual Hosting; all the requests coming to this vService are redirected to the canonical name with the ready-made Always condition.
- Where it is opened vService → Virtual Hosting · Load Balancer → Actions
- Requirement The Virtual hosting list is filled in on all vServices that share the same IP and port
- vService type HTTP
Step-by-Step Configuration
The existing vService that uses the same IP and port is opened with Edit.
Only the canonical name (www.example.com) is written in the Virtual Hosting list. The list must be filled on all the vServices that share the same IP and port (Can I publish more than one website behind a single public IP?).
Save is clicked.
Load Balancer → Actions → Add is clicked.
Action Name is entered, and Action Type = Location Redirection is selected; Redirect Code = 301: Moved Permanently, Redirect Location = https://www.example.com + Path + query of the request, Conditions = the ready-made Always condition.
Add is clicked.
Load Balancer → Load Balancer vService List → Add is clicked; the HTTP vService form opens (how to add).
In the frontend row, the same IP and port as the existing vService and a protocol in the same encryption class are selected; for HTTPS, a certificate that covers the example.com name is selected in the Certificate field (if there is no certificate, it is uploaded first: How do I upload my certificate to TR7; which formats are supported?). Only example.com is written in the Virtual Hosting list; the action from step 5 is selected in the Details → Actions field.
Save is clicked.
Verification
curl -sI https://example.com/sayfa
301 + Location: https://www.example.com/sayfaMethod 4: WEB-CLI
- How it works The non-canonical name is caught with regex in a URL condition that carries the full address of the request (scheme, Host, path and query); the Location redirection action and the vService binding are created with WEB-CLI commands. The result is the same as Method 1.
- Where it is opened TR7 WEB-CLI →
lb condition·lb action·lb service - Requirement WEB-CLI access; the condition ID
- vService type HTTP
Step-by-Step Configuration
To add www, the URL condition catches the example.com name (to remove www, the pattern "^https?://www\.example\.com/" is written):
lb condition add criteria url query "^https?://example\.com/" --content_placement regex --caseInsensitive true --name www-siz-ad| URL | Result |
|---|---|
http://example.com/sayfa?x=1, https://EXAMPLE.COM/a | Matches |
https://www.example.com/a, https://api.example.com/a, https://example.com.tr/a | Does not match |
https://example.com:8443/a | Does not match (on a non-standard port, Host also carries the port) |
The action binds the condition by its ID, not by its name. lb condition add gives no output when it succeeds; the ID is seen in the id column of the condition list:
lb condition list%PATHQ is the Path + query of the request variable; --redirectCode is always written (if it is not written, the command line default of 300 is saved):
lb action add type req_redirect_location name www-ekle redirectLoc "https://www.example.com%PATHQ" --redirectCode 301 --cond <condition-id>lb service show <vService>The rules.0, rules.1 … lines in the output show the actions on the vService in order. --rules.N replaces the action at that position; the new action is written to the first empty position.
If the vService has two actions (0 and 1), the new action is written to position 2; if it has no actions, --rules.0 is used:
lb service update id <vService> --rules.2 www-ekleVerification
curl -sI "http://example.com/sayfa?x=1"
301 + Location: https://www.example.com/sayfa?x=1Common Mistakes
- Overlooking the certificate. An HTTPS request completes the TLS handshake before it receives the redirection; the certificate must cover both names.
*.example.comdoes not cover the bareexample.comname. - Dropping the path and query. If Path + query of the request is not added to Redirect location, every request goes to the home page.
- Writing a condition that also covers the target. A Host Includes
example.comcondition also catcheswww.example.com, and the redirection enters a loop; Exact match is used. - Forgetting the non-standard port. On ports such as 8443, the Host value also includes the port (
example.com:8443); Exact matchexample.comdoes not match this request (Can I route based on the Host Header?). - Checking Case insensitive in Method 2. A name starting with
WWW.matches the condition but does not match the^www\.pattern; the request is redirected to the same name without changing. - Not solving the move from HTTP to HTTPS separately. The www redirection covers only the non-canonical name; for requests coming to the canonical name over HTTP: How do I redirect HTTP to HTTPS?
How it is done in the documentation
