An open API service indexing awesome lists of open source software.

https://github.com/tomeraberbach/redirect-url

🎯 Simple rule-based redirecting from one URL to another.
https://github.com/tomeraberbach/redirect-url

http https mapping npm-module npm-package path redirect redirect-urls redirects server url

Last synced: 7 months ago
JSON representation

🎯 Simple rule-based redirecting from one URL to another.

Awesome Lists containing this project

README

          

**redirect-url** • [**Docs**](globals.md)

---


redirect-url



version


CI


gzip size


brotli size


Sponsor


Simple rule-based redirecting from one URL to another.

## Features

- **Flexible:** completely framework agnostic
- **Powerful:** specify redirects using
[`path-to-regexp` patterns](https://github.com/pillarjs/path-to-regexp?tab=readme-ov-file#parameters)
- **Readable:** configure using a
[Netlify `_redirects`](https://docs.netlify.com/routing/redirects#syntax-for-the-redirects-file)-like
syntax

## Install

```sh
$ npm i redirect-url
```

## Usage

```js
import { createRedirectUrl, parseRedirectUrl } from 'redirect-url'

let redirectUrl = createRedirectUrl([
// Nice :)
[`/bliss`, `https://www.youtube.com/watch?v=dQw4w9WgXcQ`],

// Other redirects...
{ from: `/home`, to: `/`, status: 307 },
[`/:splat*\\.html`, `/:splat*`],
])
// OR
redirectUrl = parseRedirectUrl(`
# Nice :)
/bliss https://www.youtube.com/watch?v=dQw4w9WgXcQ

# Other redirects...
/home / 307
/:splat*\\.html /:splat*
`)

console.log(redirectUrl(`https://example.com/bliss`))
//=> { url: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', status: 302 }
console.log(redirectUrl(`https://example.com/home`))
//=> { url: 'https://example.com', status: 307 }
console.log(redirectUrl(`https://example.com/about-me.html`))
//=> { url: 'https://example.com/about-me', status: 302 }
console.log(redirectUrl(`https://example.com/spaghetti`))
//=> null
```

This package can be used with any server or framework, but see some example
integrations below. Feel free to send pull requests for more examples!

### Express

```js
const redirectsMiddleware = (req, res, next) => {
const result = redirectUrl(req.url)
if (result) {
res.redirect(result.status, result.url)
}
next()
}

app.all(`*`, redirectsMiddleware)
```

### Remix

[**entry.server**](https://remix.run/docs/en/main/file-conventions/entry.server):

```js
const handleRequest = request => {
const result = redirectUrl(request.url)
if (result) {
return redirect(result.url, result.status)
}

// ...
}

export default handleRequest
```

## API

[See here!](https://github.com/TomerAberbach/redirect-url/blob/main/docs/modules.md)

## Contributing

Stars are always welcome!

For bugs and feature requests,
[please create an issue](https://github.com/TomerAberbach/redirect-url/issues/new).

## License

[MIT](https://github.com/TomerAberbach/redirect-url/blob/main/license) ©
[Tomer Aberbach](https://github.com/TomerAberbach) \
[Apache 2.0](https://github.com/TomerAberbach/redirect-url/blob/main/license-apache) ©
[Google](https://github.com/TomerAberbach/redirect-url/blob/main/notice-apache)