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.
- Host: GitHub
- URL: https://github.com/tomeraberbach/redirect-url
- Owner: TomerAberbach
- License: apache-2.0
- Created: 2024-04-10T02:16:35.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-15T03:37:55.000Z (12 months ago)
- Last Synced: 2025-03-18T16:13:21.247Z (7 months ago)
- Topics: http, https, mapping, npm-module, npm-package, path, redirect, redirect-urls, redirects, server, url
- Language: TypeScript
- Homepage: https://npm.im/redirect-url
- Size: 590 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: docs/readme.md
- License: license-apache
Awesome Lists containing this project
README
**redirect-url** • [**Docs**](globals.md)
---
redirect-url
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)