Skip to contents

Resolves a relative or absolute URL reference against a base URL following the RFC 3986 section 5 reference-resolution algorithm, then canonicalizes the result with the same machinery as safe_parse_url. The base-merge step (empty reference, fragment-only, query-only, scheme-relative //host reference, absolute-path reference, and relative-path merge) is identical under both standards; url_standard and any ... options flow straight through to the parse so the host IPv4/reg-name model, path percent/dot-segment handling, default-port elision, WHATWG backslash-as-slash recognition, and diagnostics are exactly those of a direct safe_parse_url() call on the resolved URL. resolve_url() introduces no per-standard behavior of its own.

Usage

resolve_url(relative_or_absolute, base_url, url_standard = NULL, ...)

Arguments

relative_or_absolute

A character vector of URL references to resolve. Each may be relative ("../b", "?q=1", "#frag", "//host/p") or already absolute ("https://host/p"); an absolute reference ignores base_url.

base_url

A character vector of base URLs, recycled against relative_or_absolute. Each base must itself be an absolute URL (carry a scheme); a relative reference resolved against a scheme-less or NA base yields NA.

url_standard

Optional standard profile forwarded to the parse: NULL (default), "rfc3986", or "whatwg". See safe_parse_url for the axes it governs. The reference- resolution merge itself does not vary between the two profiles.

...

Additional arguments forwarded to safe_parse_urls (e.g. port_handling, query_handling, host_encoding). Passing a governed low-level knob that conflicts with url_standard errors, exactly as it does for safe_parse_url.

Value

A character vector the same length as the recycled inputs: the canonical clean_url of each resolved reference, or NA where resolution cannot produce an absolute URL or the resolved URL is unparseable.

Details

The return value is the canonical clean_url of the resolved reference, not a verbatim RFC 3986 recomposition: as everywhere else in rurl, the fragment and userinfo are excluded from clean_url, the query is included only when query_handling != "drop" (the default drops it), and the port only when port_handling != "exclude". This differs from a generic resolver such as xml2::url_absolute() or Python's urljoin, which preserve every component verbatim; resolve_url() resolves and canonicalizes. To inspect individual resolved components (including the fragment), resolve first and pass the result to safe_parse_url.

Examples

resolve_url("../g", "http://a/b/c/d;p?q") # -> "http://a/b/g"
#> [1] "http://a/b/g"
resolve_url("g", "http://a/b/c/d;p?q") # -> "http://a/b/c/g"
#> [1] "http://a/b/c/g"
resolve_url("//example.org/p", "http://a/b/c") # -> "http://example.org/p"
#> [1] "http://example.org/p"
resolve_url("https://x.com/y", "http://a/b/c") # absolute ref, base ignored
#> [1] "https://x.com/y"
resolve_url(c("g", "../h"), "http://a/b/c/") # vectorized
#> [1] "http://a/b/c/g" "http://a/b/h"