An express middleware that interprets .htaccess rewrite rules.
npm i express-htaccess-middleware
var path = require('path');
var express = require('express');
var RewriteMiddleware = require('express-htaccess-middleware');
var RewriteOptions = {
file: path.resolve(__dirname, '.htaccess'),
verbose: (process.env.ENV_NODE == 'development'),
watch: (process.env.ENV_NODE == 'development'),
};
var app = express();
app.use(RewriteMiddleware(RewriteOptions));
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
If the verbose option is set to true, the rules not understood by this module will be shown in the console. The redirections will also be displayed.
This is very useful to debug your redirections when developing your app.
If the watch option is set to true, the rewrite rules will be automatically reloaded whenever the htaccess file is modified.
The RewriteEngine on
directive enables the rewriting engine.
Sets the base URL for rewrites.
Defines a condition under which rewriting will take place. The pattern has to be a regular expression.
-
%{REQUEST_METHOD}
-
%{REQUEST_URI}
-
%{THE_REQUEST}
-
%{HTTP_HOST}
-
%{HTTP_USER_AGENT}
-
%{HTTP_REFERER}
-
%{QUERY_STRING}
-
%{ENV:...}
-
NC
-
OR
Defines rules for the rewriting engine
-
NC
-
R
-
N
-
L
-
F
-
G
-
QSA
-
QSD
-
S=x