is-relative-url
v4.1.1
Published
Check if a URL is relative
Downloads
2,491,194
Maintainers
Readme
is-relative-url
Check if a URL is relative
Install
npm install is-relative-urlUsage
import isRelativeUrl from 'is-relative-url';
isRelativeUrl('foo/bar');
//=> true
isRelativeUrl('https://sindresorhus.com/foo/bar');
//=> false
isRelativeUrl('//sindresorhus.com');
//=> trueAPI
isRelativeUrl(url, options?)
url
Type: string
The URL to check.
options
Type: object
allowProtocolRelative
Type: boolean
Default: true
Allow protocol-relative URLs (e.g., //example.com) to be considered relative.
Setting this to false treats URLs starting with two slash or backslash separators (//, \\, /\, \/) as absolute.
This does not prevent open redirects. For redirect validation, parse against a fixed trusted HTTP(S) base, reject parse failures or mismatched origins/protocols, and use the parsed URL without further changes.
[!NOTE] Protocol-relative URLs are technically relative according to RFC 3986, as they require the current page's protocol to resolve into absolute URLs. However, they can still navigate to external domains, which may be a security concern in certain contexts (e.g., preventing open redirects).
Related
See is-absolute-url for the inverse.
