You can simplify a bunch of the code in here with some regex, which will make it a lot more readable:

```javascript
// Bail if s is already prefixed.
if(/^\/virtual\//.test(s)) {
  return s;
}
// Make sure s has a leading / then add prefix and return
s = s.replace(/^[/]?/, '/');
return '/virtual' + s;
```