How do I safely join relative url segments?

前端 未结 11 1913
南方客
南方客 2020-12-30 19:27

I\'m trying to find a robust method of joining partial url path segments together. Is there a quick way to do this?

I tried the following:

puts URI::         


        
11条回答
  •  情话喂你
    2020-12-30 20:27

    My understanding of URI::join is that it thinks like a web browser does.

    To evaluate it, point your mental web browser to the first parameter, and keep clicking links until you browse to the last parameter.

    For example, URI::join('http://example.com/resource/', '/edit', '12?option=test'), you would browse like this:

    • http://example.com/resource/, click a link to /edit (a file at the root of the site)
    • http://example.com/edit, click a link to 12?option=test (a file in the same directory as edit)
    • http://example.com/12?option=test

    If the first link were /edit/ (with a trailing slash), or /edit/foo, then the next link would be relative to /edit/ rather than /.

    This page possibly explains it better than I can: Why is URI.join so counterintuitive?

提交回复
热议问题