URL Rewrite Based on Source IP
- Last Updated: August 5, 2025
- 2 minute read
- LoadMaster
- LoadMaster GA
- Documentation
In some cases it may be required to rewrite a rule based on a source IP subnet. For example, if there are two different websites (A and B) on one webserver and, depending on the source subnet, the user should be redirected to either website A or B. The user is using the same external URL, for example aaa.bbb.com, but will get a different website based on the source IP.
Solution
This can be done one of two ways, but either will start the same. First, set up a conditional rule using the following parameters:
Rule Name: Subnet_A
Rule Type: Content Matching
Match Type: Regular Expression
Header Field: src-ip
Match String: /^10\.0\..*/
Set Flag If Matched: Flag 1
The subnet that needs to be rewritten should be entered in the Match String. It must be done as a “classful” address as the LoadMaster is using a pseudo-header “src-ip” to do a text match against the source IP of the request. That means that something like 192.168.0/17 cannot be used – instead, use something like /192\.168\.10\..*/ to match an entire Class A, B, or C subnet.
Flag 1 is set if the above rule is matched. This can invoke another rule when matched. The rewrite can be done in two ways. Both are below.
Rule Name: Rewrite_Host
Rule Type: Replace Header
Header Field: Host
Match String: /.*/
Replace String: new.host.com
Perform If Flag Set: Flag 1
OR
Rule Name: Rewrite_URL
Rule Type: Modify URL
Match String: /.*/
Replace String: /new\0
Perform If Flag Set: Flag 1
Either of the two rewrite rules above can be used to either change the host header or the URL depending on how it needs to be changed on the server. We recommend using the hostname option because it usually results in fewer issues.
Once the two rules have been created, navigate to the Virtual Service modify screen. In the Advanced Properties section, click Show Selection Rules and apply the Subnet_A rule. Then, click Show Header Rules and apply the rewrite rule. Now, the rewrite should be applied only to requests from the designated subnet.