Hi Anderas,
Based on the solution you provided to make changes in the config file, I have implemented a solution to redirect Moodle web services to port 12345 and the Moodle web UI to port 443 by making the $CFG->wwwroot dynamic.
In Moodle, web service URLs typically include the string "/webservice/rest/server.php". By using this information, I have added conditions to the code that check if the current PHP file being executed is the web service endpoint. If it matches, the code redirects to the desired port 12345. Otherwise, if it's not a web service URL, the code redirects to port 443.
Please review and verify the following implementation for redirecting API traffic. Kindly let me know if any corrections are needed or if this solution adequately satisfies the requirement.
$CFG->wwwroot = 'https://'.(array_key_exists('SERVER_NAME', $_SERVER) ? $_SERVER["SERVER_NAME"] : 'domainname.com'); - initialize
// Set the value of $CFG->wwwroot based on the current request
if ($_SERVER['PHP_SELF'] == '/webservice/rest/server.php') {
if ($_SERVER['SERVER_PORT'] != '12345') {
$redirect = 'https://' . $_SERVER["SERVER_NAME"] . ':12345' . $_SERVER['REQUEST_URI'];
$CFG->wwwroot = $redirect;
}
} else {
if ($_SERVER['SERVER_PORT'] != '443') {
$redirect = 'https://' . $_SERVER["SERVER_NAME"] . ':443' . $_SERVER['REQUEST_URI'];
$CFG->wwwroot = $redirect;
}
}