Add x-cf-forwarded-url routing
diff --git a/scripts/lua/lib/redis.lua b/scripts/lua/lib/redis.lua
index 9275993..fcde99c 100644
--- a/scripts/lua/lib/redis.lua
+++ b/scripts/lua/lib/redis.lua
@@ -108,6 +108,8 @@
       gatewayPath = gatewayPath:sub(1,1) == "/" and gatewayPath:sub(2) or gatewayPath
       local redisKey = utils.concatStrings({"resources:", existingAPI.tenantId, ":", gatewayPath})
       _M.deleteResource(red, redisKey, REDIS_FIELD)
+      local indexKey = utils.concatStrings({"resources:", existingAPI.tenantId, ":__index__"})
+      _M.deleteResourceFromIndex(red, indexKey, redisKey)
     end
   end
   -- Add new API
diff --git a/scripts/lua/policies/backendRouting.lua b/scripts/lua/policies/backendRouting.lua
index ceee07c..81e0272 100644
--- a/scripts/lua/policies/backendRouting.lua
+++ b/scripts/lua/policies/backendRouting.lua
@@ -53,7 +53,9 @@
       u = url.parse(utils.concatStrings({'http://', dynamicBackend}))
     end
     if utils.tableContains(whitelist, u.host) then
-      ngx.req.set_uri(getUriPath(u.path))
+      if header:lower() ~= 'x-cf-forwarded-url' then
+        ngx.req.set_uri(getUriPath(u.path))
+      end
       setUpstream(u)
     else
       request.err(403, 'Dynamic backend host not part of whitelist.')
@@ -87,4 +89,4 @@
   ngx.var.upstream = upstream
 end
 
-return _M
\ No newline at end of file
+return _M
diff --git a/scripts/lua/routing.lua b/scripts/lua/routing.lua
index e36fd12..9143487 100644
--- a/scripts/lua/routing.lua
+++ b/scripts/lua/routing.lua
@@ -22,6 +22,7 @@
 -- Used to dynamically handle nginx routing based on an object containing implementation details
 
 local cjson = require "cjson"
+local url = require "url"
 local utils = require "lib/utils"
 local request = require "lib/request"
 local redis = require "lib/redis"
@@ -89,6 +90,12 @@
   -- Check for exact match or case where resource is "/"
   local redisKey = utils.concatStrings({"resources:", tenant, ":", path})
   local redisKeyWithSlash = utils.concatStrings({redisKey, "/"})
+  -- Check for x-cf-forwarded-url
+  local cfUrl = ngx.req.get_headers()["x-cf-forwarded-url"]
+  if cfUrl ~= nil and cfUrl ~= "" then
+    local u = url.parse(cfUrl)
+    redisKey = utils.concatStrings({"resources:", tenant, ":", path, u.path})
+  end
   for _, key in pairs(resourceKeys) do
     if key == redisKey or key == redisKeyWithSlash then
       local res = {string.match(key, "([^:]+):([^:]+):([^:]+)")}
@@ -97,6 +104,7 @@
     end
   end
   -- Construct a table of redisKeys based on number of slashes in the path
+  local redisKey = utils.concatStrings({"resources:", tenant, ":", path})
   local keyTable = {}
   for i, key in pairs(resourceKeys) do
     local _, count = string.gsub(key, "/", "")
diff --git a/tests/fakengx.lua b/tests/fakengx.lua
index add7f71..8f40805 100644
--- a/tests/fakengx.lua
+++ b/tests/fakengx.lua
@@ -476,6 +476,10 @@
   function ngx.req.read_body()
   end
 
+  function ngx.req.get_headers()
+    return {}
+  end
+
   -- http://wiki.nginx.org/HttpLuaModule#ngx.socket.tcp
   function ngx.socket.tcp()
     local sock = TCP:new()