This topic describes how to configure domain name- and URL path-based forwarding rules for Application Load Balancer (ALB) listeners, and how to configure advanced URL path-based forwarding rules for rewrites and redirects.
Domain name-based forwarding rules
You can specify a specific domain name, a wildcard domain name, or a regular expression as the match condition in a domain name-based forwarding rule. For more information about how to create a forwarding rule, see the Create a forwarding rule section of the Manage forwarding rules for a listener topic.
Match type | Description |
Exact match and wildcard match |
|
Regular expression match |
|
URL-based forwarding rules
You can specify a specific URL, a wildcard URL, or a regular expression as the match condition in a URL-based forwarding rule. For more information about how to create a forwarding rule, see the Create a forwarding rule section of the " Manage forwarding rules for a listener" topic.
Match type | Description |
Exact match and wildcard match |
|
Regular expression match |
|
Advanced URL-based forwarding rule settings for rewrites and redirects
If you specify a regular expression as the match condition in a rewrite or redirect rule, the original URL is overwritten by the variables that match the regular expression. For more information about how to create a forwarding rule, see the Create a forwarding rule section of the Manage forwarding rules for a listener topic.
For more information about how to configure regular expressions in URL-based forwarding conditions, see the URL-based forwarding rules section of this topic.
Usage notes
You can specify one or more regular expressions as the match conditions in a rewrite or redirect rule and use parentheses
( )
to define them as capture groups. The number of capture groups must be the same as the number of variables in the rewrite or redirect rule.You can use up to three variables (
${1}
,${2}
, and${3}
) to form the URL that overwrites the original URL. The variables cannot be replaced with other characters.
Procedure
URL matching: The client sends a request. The request matches a regular expression that is specified in a forwarding rule.
Extraction and replacement: extracts the content from the capture groups
( )
and writes the content to${1}
,${2}
, and${3}
. The content is used to replace the rewriting or redirection path of the forwarding action.Splicing: splices the strings that overwrote the variables
${1}
,${2}
, and${3}
to form the new URL.
No.
Step
Example
1
Configure the forwarding condition and the forwarding action in a forwarding rule.
Path in the forwarding condition:
/sys/(.*)/(.*)/aaa
Path to which requests are rewritten or redirected:
/${1}/${2}
2
A client request matches the path in the forwarding rule.
Requested path:
/sys/ccc/bbb/aaa
Matched path:
/sys/(.*)/(.*)/aaa
3
The system extracts values from the regular expression and replaces the variables with the values.
ccc
andbbb
are extracted from(.*)
in the regular expression and replace the variables ${1} and ${2} in the path to which requests are rewritten or redirected.${1}
is replaced byccc
.${2}
is replaced bybbb
.
4
The values are concatenated to form the actual path.
The path received by the backend server is
/ccc/bbb
.Examples
You can create forwarding rules in the ALB console based on the usage notes and procedure.
Example 1: Configure the Rewrite or Forward action
In this example, a forwarding rule is configured to rewrite the path of requests destined for
sys/ccc/bbb/aaa
to/ccc/bbb
and forward the requests accordingly. The following figure shows the sample configuration.Parameter
Description
If Match All Conditions
Regular expression match
Match conditions
The requested path matches the specified regular expression.
Requirements
The regular expression can contain only letters, digits, and the following special characters:
. - _ / = ? ~ ^ * $ : ( ) [ ] + |
.Examples
Requested path:
/sys/ccc/bbb/aaa
Case-sensitive regular expression: The requested path is matched if
/sys/(.*)/(.*)/aaa
is specified in the forwarding rule.Case-insensitive regular expression: The requested path is matched if
/sys/(.*)/(.*)/aaa
is specified in the forwarding rule.
Action
Rewrite
Domain Name:
${host}
Path:
/${1}/${2}
Search:
${query}
NoteSet Search to the part follows the question mark (?) in a URL.
Example: To rewrite URLs to
www.example.com/test/test1?x=1
, set Search tox=1
.Forward
Select a server group from the drop-down list.
Example 2: Configure the Redirect action
In this example, a forwarding rule is configured to redirect requests originally destined for
sys/ccc/bbb/aaa
to/ccc/bbb
. The following figure shows the sample configuration.Parameter
Description
If Match All Conditions
Regular expression match
Match conditions
The requested path matches the specified regular expression.
Requirements
The regular expression can contain only letters, digits, and the following special characters:
. - _ / = ? ~ ^ * $ : ( ) [ ] + |
.Examples
Requested path:
/sys/ccc/bbb/aaa
Case-sensitive regular expression: The requested path is matched if
/sys/(.*)/(.*)/aaa
is specified in the forwarding rule.Case-insensitive regular expression: The requested path is matched if
/sys/(.*)/(.*)/aaa
is specified in the forwarding rule.
Action
Redirect
Protocol:
$protocol
Domain Name:
${host}
Port:
${port}
Path:
/${1}/${2}
Search:
${query}
Status Code:
301
FAQ
How can I configure a forwarding rule to remove a specific prefix from the request URL path and forward the request based on the subsequent directories?
Solution:
Assume you want ALB to remove /api
from the URL of requests destined for www.example.com/api/aaa/bbb/...
and forward the requests based on the directories following /api
.
In the forwarding rule, set Add Condition to Path, select Regular Expression Matching, and set the path to /api/(.*)
to match all requests whose URL path starts with /api
. Set Action to Rewrite or Redirect, and set the action Path to /${1}
to use the part following /api
in the original URL path.
Sample configurations are as follows:
Rewrite
Redirect
References
For more information about how to configure other forwarding rules for an ALB listener, see Manage forwarding rules for a listener.