When an URL is shortened, a unique hash is generated based on the original URL, meaning that "site_id/relative/resource.pdf" and "http://weblearn.ox.ac.uk/site_id/relative/resource.pdf" are completely different and generate two different short URLs.

---

Say that `1234` is the hash for `http://weblearn.ox.ac.uk/site_id/relative/resource.pdf`, `abcd` is the hash for `second_site_id/relative/resource.pdf` and `9876` is the hash for `http://weblearn.ox.ac.uk/second_site_id/relative/resource.pdf`

Here is how the conversion of `<a href="http://weblearn.ox.ac.uk/x/1234">http://weblearn.ox.ac.uk/x/1234</a>` would work:

    // 1/ replace on HTML attributes
         <a href="http://weblearn.ox.ac.uk/x/1234">http://weblearn.ox.ac.uk/x/1234</a>
    // 1.1/ expand the URL if possible
         <a href="http://weblearn.ox.ac.uk/site_id/relative/resource.pdf">http://weblearn.ox.ac.uk/x/1234</a>
    // 1.2/ replace the site_id with the new one (and with the current code, aslo reduce the URL)
         <a href="second_site_id/relative/resource.pdf">http://weblearn.ox.ac.uk/x/1234</a>
    // 1.3/ re-shorten the URL
         <a href="http://weblearn.ox.ac.uk/x/abcd">http://weblearn.ox.ac.uk/x/1234</a>

    // 2/ replace URLs with a site_id based on a UUID
    // 2.1/ expand shortened URL
         <a href="http://weblearn.ox.ac.uk/x/abcd">http://weblearn.ox.ac.uk/site_id/relative/resource.pdf</a>
    // 2.2/ replace the site_id with the new one
         <a href="http://weblearn.ox.ac.uk/x/abcd">http://weblearn.ox.ac.uk/second_site_id/relative/resource.pdf</a>
    // 2.3/ re-shorten the URL
         <a href="http://weblearn.ox.ac.uk/x/abcd">http://weblearn.ox.ac.uk/x/9876</a>

There were two solutions, the first one (this one) is to limit the change to the URL to the site ID, the second solution would be trying to reduce the URL either before or after 2.2