Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ var rwGateway = function (dir){
rule: '^/$',
cgi: '/usr/local/opt/php54/bin/php-cgi',
to: '/index.php'
},

/* Use the following options to pass original uri to target script. Relative URI doesn't have a leading slash. */
{
rule: '^.*$',
cgi: '/usr/local/opt/php54/bin/php-cgi',
to: '/index.php',
query: 'uri={{URI}}&relativeUri={{RELATIVE_URI}}&{{QUERY}}'
}
]}
);
Expand Down
22 changes: 21 additions & 1 deletion gateway-rewrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ module.exports = function gateway_rewrite(docroot, options) {
}
}

function replaceStringItems(sourceString, items) {
if (!sourceString || !items)
return sourceString || '';

var result = sourceString;

Object.keys(items).forEach(function(key){
var regexp = new RegExp('\{\{'+key+'\}\}','g');
result = result.replace(regexp,items[key]);
});

result = result.replace(/\{\{.*\}\}/g, ''); // Remove items with no value.
return result;
}

var matches = 0;
var url = URL.parse(req.url)

Expand All @@ -57,6 +72,11 @@ module.exports = function gateway_rewrite(docroot, options) {
var file = options.rules[j].to
var uri = url.pathname
var path = normalize(join(docroot, file))
var query = replaceStringItems(options.rules[j].query, {
RELATIVE_URI: url.pathname.replace(/^\//,''),
URI: url.pathname,
QUERY: url.query || ''
});

// populate the environment
var host = (req.headers.host || '').split(':')
Expand All @@ -73,7 +93,7 @@ module.exports = function gateway_rewrite(docroot, options) {
SCRIPT_FILENAME: path,
PATH_TRANSLATED: path,
REQUEST_METHOD: req.method,
QUERY_STRING: url.query || '',
QUERY_STRING: query,
GATEWAY_INTERFACE: 'CGI/1.1',
SERVER_PROTOCOL: 'HTTP/1.1',
PATH: process.env.PATH,
Expand Down