diff --git a/README.md b/README.md index ad163ae..cc355f5 100644 --- a/README.md +++ b/README.md @@ -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}}' } ]} ); diff --git a/gateway-rewrite.js b/gateway-rewrite.js index d96014d..5c06ba1 100644 --- a/gateway-rewrite.js +++ b/gateway-rewrite.js @@ -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) @@ -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(':') @@ -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,