A lws middleware plugin to perform a 302 Redirect if the request URL matches a specified regular expression.
Adds the following option to lws.
--redirect expression ... A list of URL redirect rules. For each rule, separate the 'from' and 'to'
expressions with '->'. Whitespace surrounding the expressions is ignored.
E.g. 'http -> https'.
Supply a "from" regular expression and "to" replace string. If the request URL matches the "from" regexp, the matching "from" string will be replaced by the "to" string.
Some examples if the incoming request URL is http://localhost:
--redirect 'http -> https'would redirect tohttps://localhost.--redirect 'http -> https' 'localhost -> remotehost'would redirect tohttps://remotehost.
$ npm install --save-dev lws-redirect
$ lws --port 80 --stack lws-redirect --redirect 'http -> https'
Listening on http://mba4.local:80, http://127.0.0.1:80, http://192.168.0.200:80
$ $ curl -I http://127.0.0.1/
HTTP/1.1 302 Found
Location: https://127.0.0.1/
Content-Type: text/html; charset=utf-8
Content-Length: 67
Date: Sun, 09 Jun 2019 16:53:38 GMT
Connection: keep-alive
This package includes a trivial command-line tool for testing redirect rules. Test each rule individually using this command structure:
$ npx lws-redirect <from> <to> <url>
The following examples test a rule which adds a trailing / to a path ending with one or two. The first two examples produce a match and modified target URL. The third and fourth examples do not produce a match so the target URL remains unmodified.
Note: Windows users may need to use double instead of single quotes.
$ npx lws-redirect '(one|two$)' '$1/' http://something.com/one
http://something.com/one/
$ npx lws-redirect '(one|two$)' '$1/' http://something.com/two
http://something.com/two/
$ npx lws-redirect '(one|two$)' '$1/' http://something.com/three
http://something.com/three
$ npx lws-redirect '(one|two)$' '$1/' http://something.com/one/four
http://something.com/one/four
© 2019-20 Lloyd Brookes <[email protected]>.