How do I redirect a specific URL to another URL with a 301/302?
Short answer
A Path condition that defines the old address (e.g. Exact Match, /eski-sayfa) is created; in the Location Redirection action, Redirect Code and Redirect Location (the new address) are entered, the condition is selected, and the action is added to the Actions field of the vService. For a permanent move, 301 is selected; for a temporary redirection, 302 is selected; if a POST request must go with the same method, 308 or 307 is selected. If Location redirection is saved without a condition, it never works.
Background
A redirection response sends the client a 3xx status code and the Location header carrying the address to go to; the browser makes a new request to this address. The codes are selected according to two questions: is the move permanent, and must requests such as POST be repeated with the same method (RFC 9110 · 15.4 Redirection 3xx).
| Code | Meaning | Method | When |
|---|---|---|---|
| 301 Moved Permanently | The resource has moved permanently; search engines index the new address, browsers may cache the response | Browsers may convert POST to GET | Permanent address change (SEO) |
| 302 Found | The resource is temporarily at another address; the old address remains valid | Browsers may convert POST to GET | Campaign, temporary maintenance, testing |
| 303 See Other | The result is at another address; the browser goes there with GET | Always GET | Result page after a form submission |
| 307 Temporary Redirect | Temporary like 302 | Method and body are preserved | POST/PUT in a temporary move |
| 308 Permanent Redirect | Permanent like 301 | Method and body are preserved | POST/PUT in a permanent move |
Problem: Pages Whose Address Changes Turn into Broken Links
When the address of a page changes, users and search engines coming to the old address receive 404; the traffic coming from old links and the search ranking are lost. The request coming to the old address must be redirected to the new address without going to the Backend Service.
Solution: Five Methods
All the methods use the same Location Redirection action; what changes is how the old address is recognized and how the new address is produced. The redirection works only on an HTTP vService.
| Your situation | Suitable method |
|---|---|
| A single page has moved to another address | Method 1: Location redirection + Path condition |
The old address is defined by a specific query string (/urun.php?id=5) | Method 2: Path + Query or URL condition |
An entire path prefix has moved (/blog/… → /haber/…) | Method 3: Prefix replacement with Replace (first) |
| There are many old address → new address mappings | Method 4: Dynamic list + Mapping |
| Configuration from the command line | Method 5: WEB-CLI |
Method 1: Location Redirection + Path Condition Recommended
- How it works A request that matches the condition does not go to the Backend Service; TR7 responds with the selected code and the new address.
- Where it is openedLoad Balancer → Conditions · Load Balancer → Actions · vService → Details → Actions
- Requirement At least one condition; Location redirection without a condition does not work
- vService type HTTP
Step-by-Step Configuration
In the menu opened with the TR7 logo at the bottom left, Load Balancer → Conditions → Add is clicked (how to add).
Criteria = Path, the matching method Exact Match (default) and the value /eski-sayfa are entered. The Path value does not include the query string; the /eski-sayfa?x=1 request also matches this condition. If the case difference is to be ignored, Case Insensitive is checked.
Add at the bottom of the form is clicked.
Load Balancer → Actions → Add is clicked (how to add).
Action Name is entered, and Action Type = Location Redirection is selected.
- Redirect Code: the default is 301: Moved Permanently. Options: 301: Moved Permanently, 302: Found (Moved Temporarily), 303: See Other, 307: Temporary Redirect, 308: Permanent Redirect.
- Redirect Location (required):
/yeni-sayfa(same domain name) orhttps://www.example.com/yeni-sayfa(another domain name). The address is used as entered; the query string of the old request is not added automatically; if it is to be preserved, the Request query variable is added to the end of the address with fx. - Conditions: the condition from step 2 is selected.
- If Hit Counter (off by default) is turned on, the redirections are counted.
Add at the bottom of the form is clicked.
On the Load Balancer → Load Balancer vService List screen, the HTTP vService is selected and Edit is clicked (Edit).
Details is opened and the action is selected in the Actions field. Actions are processed in their order in the list; since the redirection ends the request, the actions after it do not run for this request (action ordering).
Save is clicked. The change is applied without dropping open connections; the progress is followed from the sync indicator at the bottom right of the screen. In a cluster, it is also sent to the peer device with the default auto-sync; if Manual Sync Mode is on, it is sent to the peer device with Synchronize.
Verification
curl -sI http://www.example.com/eski-sayfa
HTTP/1.1 301 Moved Permanently + Location: /yeni-sayfaIf the hit counter is on, the count of the action increases in the Action Hits tab of the vService dashboard; on the HTTP Logs screen, the request appears with the 301 status code.
Count increases · status code 301Method 2: Path + Query or URL Condition
- How it works If the old address is defined by a specific query string (
/urun.php?id=5), the condition compares the path and the query together; only that request is redirected. - Where it is openedLoad Balancer → Conditions → Path + Query or URL
- Requirement The query string must always arrive in the same order in the request (Exact match compares exactly)
- vService type HTTP
Step-by-Step Configuration
Load Balancer → Conditions → Add is clicked.
Criteria = Path + Query, the matching Exact match and the value /urun.php?id=5 are entered. If the domain name is also to be distinguished, Criteria = URL is selected and the value is written together with the scheme: http://www.example.com/urun.php?id=5. Since the URL value also includes the scheme of the request, a separate https://www.example.com/urun.php?id=5 condition is added for the request arriving over HTTPS, and the two conditions are selected with or in the action.
Add is clicked.
Load Balancer → Actions → Add is clicked.
Location Redirection is defined as in step 5 of Method 1; Redirect Location = /urunler/5, and this condition 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.
Verification
curl -sI "http://www.example.com/urun.php?id=5" and, for comparison, curl -sI "http://www.example.com/urun.php?id=6"
id=5 → 301 + Location: /urunler/5 · id=6 → no redirectionMethod 3: Prefix Replacement with Replace (first)
- How it works Redirect location takes the path of the request as a Smart Variable and changes its prefix with the Replace (first) function; every page under
/blog/goes to its counterpart under/haber/. - Where it is openedLocation Redirection → Redirect Location → fx
- Requirement The path structure apart from the prefix must stay the same at the new address
- vService type HTTP
Step-by-Step Configuration
Load Balancer → Conditions → Add is clicked.
Criteria = Path, the matching Start and the value /blog/ are entered. Case insensitive is not checked: the ^/blog/ pattern in the function is case-sensitive; if the condition also catches the /BLOG/yazi request, the request is redirected to the same place without the address changing, and a loop occurs.
Add is clicked.
Load Balancer → Actions → Add is clicked.
Action Name is entered, and Action Type = Location Redirection and Redirect Code = 301: Moved Permanently are selected.
With fx in the Redirect Location field, the Path/page of the request [URI] variable is added (Variables).
In the variable window, Add Functions is opened and Replace (first) is selected: To Replace = ^/blog/, New Value = /haber/. The To Replace field is a regular expression; ^ changes only the /blog/ part at the start of the path.
In the same field, the Request query variable is added after the path variable with fx. This variable gives the query string together with its leading ? and stays empty if there is no query; a separate ? is not written in between.
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:
| Request | Location |
|---|---|
/blog/yazi-1 | /haber/yazi-1 |
/blog/2024/ocak/yazi?utm=x&p=2 | /haber/2024/ocak/yazi?utm=x&p=2 |
/blog/ | /haber/ |
/blog/blog/x | /haber/blog/x (only the leading prefix changes) |
/blog (no trailing /) | The condition does not match; it is not redirected. If this address is also to be moved, a separate redirection for Exact match /blog is added with Method 1 |
Verification
curl -sI "http://www.example.com/blog/2024/yazi?p=2"
301 + Location: /haber/2024/yazi?p=2Method 4: Bulk Mapping with Dynamic List + Mapping
- How it works The old paths are searched in a Dynamic List condition; Redirect location converts each old path to its own new path with the Mapping (exact match) function. Hundreds of mappings are managed with a single condition and a single action.
- Where it is openedConditions → Dynamic List · Redirect Location → fx → Mapping (exact match)
- Requirement Each old path in the mapping list must also be in the list of the condition; the paths must not contain spaces
- vService type HTTP
Step-by-Step Configuration
Load Balancer → Conditions → Add is clicked.
Criteria = Dynamic List is selected. The default client IP variable in the Key to Search field is deleted and Path/page of the request [URI] is added with fx; Search Method = Exact match; the old paths are written in the List field, one per line (/urun-a, /urun-b, /hakkimizda.html).
Add is clicked.
Load Balancer → Actions → Add is clicked.
Action Type = Location Redirection and Redirect Code = 301: Moved Permanently are selected.
With fx in the Redirect Location field, Path/page of the request [URI] is added, Add functions → Mapping (exact match) is selected, and eski-yol yeni-yol is written in the list, one per line:
/urun-a /urunler/a
/urun-b /urunler/b
/hakkimizda.html /kurumsal/hakkimizdaThen the Request query variable is added to preserve the query string.
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.
If no line matches in Mapping, the result is empty; therefore, the condition list and the mapping list must contain the same old paths. A request such as /urun-a/ (trailing /) matches neither the list nor the mapping and is not redirected.
Verification
Each old path in the list is requested with curl -sI; a path that is not in the list is also tried.
/urun-a → 301 + Location: /urunler/a · a path not in the list is not redirectedMethod 5: WEB-CLI
- How it works The condition, the 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
The Path condition is added; if no matching is specified, Exact match is used:
lb condition add criteria path query /eski-sayfa --name eski-sayfaThe action binds the condition not by its name but by its ID. lb condition add gives no output when it succeeds; the ID is seen in the id column of the condition list, next to the name of the condition:
lb condition listlb action add type req_redirect_location name eski-yeni redirectLoc /yeni-sayfa --redirectCode 301 --cond <condition-id>--redirectCode is always written; if it is not written, 300 (Multiple Choices), the command line default, is saved; the 301 default of the interface does not apply here. If --cond is not written, the action is saved but never runs.
lb service show <vService>The rules.0, rules.1 … lines in the output show the actions on the vService in order. In the lb service update command, --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 eski-yeniVerification
The rules.N lines in the lb service show <vService> output are checked; then curl -sI http://www.example.com/eski-sayfa is tried.
rules.2 = eski-yeni, previous actions in place · 301 + Location: /yeni-sayfaCommon Mistakes
Location redirection without a condition does not work
Location redirection is also saved without a condition being selected, but it does not redirect any request; the interface does not give a warning in this case. At least one condition is selected for every redirection (Why doesn't a Location Redirection or User Authentication (On HTTP request) action work if I add it without a condition?).
- Assuming that the query string is carried over automatically. Redirect location is used as entered; if the query string is to be preserved, the Request query or Path + query of the request variable is added.
- Testing with 301. Browsers cache 301; even if a wrong rule is corrected, the visitor may continue to go to the old target. The rule is first tested with 302 and changed to 301 after it is verified.
- Redirecting POST requests with 301/302. With these codes, browsers may convert POST to GET, and the form data is lost; if the method is to be preserved, 308 or 307 is selected.
- Creating a loop. If the new address comes to the same vService and meets the condition again (e.g. a Path Start
/uruncondition and the target/urunler/…), the request is redirected endlessly; TR7 does not prevent this. The condition is kept narrow so that it does not cover the target address. - Checking Case insensitive in a prefix redirection. The Replace (first) pattern is case-sensitive; a request with uppercase letters is redirected to the same address without changing, and a loop occurs.
- Trying it on a Network or L7 TCP vService. Location redirection appears only in the Actions list of an HTTP vService.
How it is done in the documentation
Related questions
- How do I set up a redirect that adds or removes www?
- How do I do an SEO-friendly migration from an old domain to a new domain?
- Can I rewrite the URL path before it reaches the backend (rewrite)?
- Why doesn't a Location Redirection or User Authentication (On HTTP request) action work if I add it without a condition?
