Redirecting to the next page in your case should not depend on the previously visited page, so you are better off finding the root cause of the wrong redirection to /p/imitate.
Questions:
Add screenshots of the customJavaRequesthandler and the microflow call to your question.
It looks like the root cause of the wrong redirection to /p/imitate is the behavior of Mendix when you set a URL in the page. I use the URLRedirector widget to call our custom Java RequestHandler at /imitate. I set the URL (prefix) in URLRedirector to imitate?name=
Looking at the URLRedirector widget Javascript on GitHub, it’s using the standard Javascript function window.location.replace(url) to redirect to imitate?name=username. If the current URL is just the Mendix default http://localhost:8080/index.html then it redirects correctly to http://localhost:8080/imitate?name=username. However if the current URL is http://localhost:8080/p/resources after visiting a page with a URL assigned, it redirects to http://localhost:8080/p/imitate?name=username, which is an invalid endpoint. Mendix must have a servlet filter or handler running behind the scenes that rewrites URLs if the current URL has a /p in it. It’s not the URLRedirector widget that’s causing the problem.
The solution is for Mendix to reset the URL to the default (for example, http://localhost:8080/index.html) when navigating to any page that does not have a URL assigned to it. There is no good reason I can think of to keep the /p/ prefix in the URL after you have navigated away from a page with a URL assigned to it. I consider this a bug.
I have developed a workaround by modifying the URLRedirector.js _redirectTo function like this:
_redirectTo : function(url) {
/**
* To work around the Mendix bug when a page is assigned a URL,
* causing the URL to be prefixed with /p/ even when navigating
* to a page that does not have a URL, construct the root URL
* from the protocol and host and prefix it to the incoming URL.
* If the incoming URL contains a protocol prefix already (http...),
* just use it rather than generating a protocol and host prefix.
*/
if (url.indexOf('http') === 0) {
if(this.Target === "Page")
{
// window.location.href = url;
window.location.replace(url);
}
else
{
window.open(url);
}
}
else {
// String leading / from input URL, if any.
while ( url.charAt(0) == "/" ) url = url.substr(1);
// Generate the root URL from the protocol and host (e.g.; http://localhost:8080)
var rootUrl = window.location.protocol + "//" + window.location.host;
if(this.Target === "Page")
{
window.location.replace(rootUrl + "/" + url);
}
else
{
window.open(rootUrl + "/" + url);
}
}
}