By the above scenario we can use the This is achievable through URL path-based routing rules and URL rewrite rules.
In your Application Gateway configuration, ensure that you have defined backend pools for your vm and static web app.
Create route rule default domain to app1 backend pool
Under Routing rules, configure a path-based rule to route requests for /app2/* to the app2 backend pool.
Repeat the same process to the root (/) continue to route to app1.
Create or update the HTTP settings to enable Override with new host name. This ensures that the host header is set to the backend pool’s host name.
Set up a URL rewrite rule to modify the incoming URL path. This rule should rewrite www.example.com/app2 to the root path of your static web app.
create a listener for the backend pool. In the listener, set the "Protocol" to HTTP, and set the "Hostname" to www.example.com. Finally, set the "Path-based routing" to "Enabled", and add a rule to route requests with the path "/app2" to the backend pool.
Reference:
Rewrite HTTP headers and URL with Azure Application Gateway | Microsoft Learn
Azure Application Gateway features | Microsoft Learn
Please don’t forget to close the thread by clicking "Accept the answer" wherever the information provided helps you, as this can be beneficial to other community members.