is-relative-url
Check if a URL is relative
Last updated 2 months ago by sindresorhus .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install is-relative-url 
SYNC missed versions from official npm registry.

is-relative-url

Check if a URL is relative

Install

npm install is-relative-url

Usage

import isRelativeUrl from 'is-relative-url';

isRelativeUrl('foo/bar');
//=> true

isRelativeUrl('https://sindresorhus.com/foo/bar');
//=> false

isRelativeUrl('//sindresorhus.com');
//=> true

API

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.

Current Tags

  • 4.1.1                                ...           latest (2 months ago)

8 Versions

  • 4.1.1                                ...           2 months ago
  • 4.1.0                                ...           a year ago
  • 4.0.0                                ...           4 years ago
  • 3.0.0                                ...           8 years ago
  • 2.0.0                                ...           11 years ago
  • 1.0.0                                ...           12 years ago
  • 0.1.1                                ...           12 years ago
  • 0.1.0                                ...           12 years ago
Maintainers (1)
Downloads
Total 23
Today 0
This Week 1
This Month 9
Last Day 0
Last Week 7
Last Month 8
Dependencies (1)
Dev Dependencies (3)

© 2010 - cnpmjs.org x YWFE | Home | YWFE