FortiWeb
A FortiWeb can be configured to join a Security Fabric through the root or downstream FortiGate.
shafiq23
Staff
Staff
Article Id 337314
Description This article describes how to rewrite the HTTP header with custom redirection using LUA script matching specific conditions according to web server return code response.
Scope FortiWeb.
Solution

Requirement:
When the HTTP response matches a specific return code, for example in this demonstration is a non-existing link or broken link (HTTP 404), redirect the client request to the website home page or maintenance page.

 

  1. Create Scripting.
    Application Delivery -> Scripting -> Create New.

    1. Define name.
    2. Input LUA script statement.
    3. Select OK.
                                              

2.PNG

 

function rewrite_response(HTTP, args)
-- rewrite status, includes code and reason
local code_new = 301
local reason_new = "Redirect"
HTTP:set_status(code_new, reason_new) -- if reason exists, set code and reason
HTTP:add_header("Location", "https://dvwa.ftntlab.local/index.php")
HTTP:collect()
end

when HTTP_RESPONSE {
local code, reason = HTTP:status()
debug("===code=%s, =====reason=%s\r\n", code, reason)
local code_first_char = string.sub(code,1,3)
debug("===code_first_char=%s\r\n", code_first_char)
if code_first_char =="404" then
local t = HTTP:priv()
return rewrite_response(HTTP, t["args"])
end
}

when HTTP_DATA_RESPONSE {
HTTP:set_body("", 0, -1)
}

 

Details and comments on the LUA script used in this article.

 

1.png

 

  1. Assign the server policy with the created script.
    Policy -> Server Policy -> Edit the respective server policy -> Enable Scripting -> Select created script.
                                                           

    3.PNG

     

     

Demonstration:
• Request a non-existing URL.
• Expect the script to add a new Location header for redirection to the website home page.

 

4.png

 

Related documentation about the Scripting:
Script Reference - HTTP commands