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.
- Create Scripting.
Application Delivery -> Scripting -> Create New.
- Define name.
- Input LUA script statement.
- Select OK.

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.

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

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

Related documentation about the Scripting: Script Reference - HTTP commands
|