0

I'd like to take the following URLs:

  • Base: https://example.com/this/is/base/resource
  • Absolute: https://example.com/this/is/up-one/other-resource?possibly-with=querystring

From there, I'd like to calculate the shortest relative URL from base. For this example, I should get:

../up-one/some-other-resource?possibly-with=querystring

Is there a standard method for doing this? I know there are a lot of edge cases, so before I build it, I wanted to see if a good method already existed.

I know the URL API can go the other way around, which is ultimately what I will be using to go back again.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • Can you offer a little back-story on _why_ you want to do this? – Phil Aug 18 '20 at 04:25
  • 1
    @Phil I have a bunch (hundreds of thousands) of signed URLs for resources in an S3 bucket. The AWS JS SDK that generates these signed URLs necessarily creates full absolute URLs. But, since I have so many and they're all relative to a main index file (which will contain the URLs), I figured I could save 50% of the index size by making relative paths. So, that's why I have to start with two absolute URLs. – Brad Aug 18 '20 at 04:32
  • Gzipping your responses will save a lot more bytes. And if you're already gzipping your response, then making a change like this will likely have a negligible impact – Phil Aug 18 '20 at 04:34
  • @Phil Yes, I already compress these index files, and the impact of using relative paths is far larger than negligible in my case. But, thanks for your input. – Brad Aug 18 '20 at 04:37
  • https://stackoverflow.com/questions/60359050/how-to-convert-absolute-url-to-relative – Kaiido Aug 19 '20 at 06:29

0 Answers0