is-relative-url

Check if a URL is relative

MIT 7 个版本
安装
npm install is-relative-url
yarn add is-relative-url
pnpm add is-relative-url
bun add is-relative-url
README

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 will treat protocol-relative URLs as absolute, which can be useful for security purposes when you want to ensure a URL won't redirect to an external domain.

[!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).

See is-absolute-url for the inverse.

版本列表
4.1.0 2025-09-10
4.0.0 2022-06-06
3.0.0 2019-04-06
2.0.0 2015-11-14
1.0.0 2014-08-13
0.1.1 2014-07-05
0.1.0 2014-07-04