Kamailio rewrite uri
A popular topic in the world of SIP!
In Kamailio (formerly OpenSER), the rewrite_uri
function is used to modify the Request-URI (R-URI) of a SIP message. This function is commonly used to perform various tasks, such as:
- URL rewriting: Modify the R-URI to point to a different destination.
- Domain rewriting: Replace a domain name in the R-URI with another domain name.
- Port rewriting: Change the port number in the R-URI.
- Path rewriting: Modify the path part of the R-URI.
The rewrite_uri
function takes three arguments:
uri
: The original R-URI.new_uri
: The new R-URI to be written.flags
: Optional flags that control the rewriting process.
Here's an example of how to use the rewrite_uri
function in a Kamailio script:
route {
...
rewrite_uri("sip:olddomain.com;transport=udp", "sip:newdomain.com;transport=tcp", lr);
...
}
In this example, the rewrite_uri
function is used to replace the oldomain.com
domain with newdomain.com
in the R-URI, and also change the transport protocol from UDP to TCP.
The lr
flag stands for "loopback" and is used to indicate that the rewriting should be done in a loopback manner, i.e., the rewritten R-URI should be used as the new R-URI for the next hop.
You can also use regular expressions to match and replace parts of the R-URI using the regmatch
and regsub
functions:
route {
...
regmatch("sip:([^;]+);transport=udp", "sip:$1;transport=tcp", uri);
...
}
In this example, the regular expression sip:([^;]+);transport=udp
matches the R-URI and captures the domain name (everything before the first ;
) using a capture group (([^;]+)
). The regsub
function is then used to replace the captured group with the new domain name, resulting in the rewritten R-URI.
These are just a few examples of how you can use the rewrite_uri
function in Kamailio. The possibilities are endless, and the function is a powerful tool for manipulating SIP messages in your Kamailio script.