Skip to main content
shafiq23
Staff & Editor
Staff & Editor
October 21, 2025

Technical Tip: How to redirect request when specified cookies are not present using a LUA script

  • October 21, 2025
  • 0 replies
  • 216 views
Description This article describes how to configure request redirection when a specified cookie is not present in an HTTP request using a LUA script.
Scope FortiWeb.
Solution

Requirement:
To redirect the client’s request to a landing or login page when a specific cookie is not present in the initial HTTP request.

 

Create Scripting: Go under Application Delivery -> Scripting -> Create New, define a name, input a Lua script statement. and select OK.

 

when HTTP_REQUEST {
  local path = HTTP:path()
  if path == "/index.php" then
    local cookie_tbl = HTTP:header("Cookie")
    local has_cookie = false
    if cookie_tbl then
      for _, v in pairs(cookie_tbl) do
        if string.find(v, "PHPSESSID=", 1, true) then
          has_cookie = true
          break
        end
      end
    else
    end

    if not has_cookie then
      return HTTP:redirect("https://%s/login.php", HTTP:host())
    else
    end
  else
  end
}

 

Details and comments for the Lua script used in this article:


2.png

 

Assign the server policy with the created script under Policy -> Server Policy > edit respective server policy, enable Scripting and select the created script.

3.png

 

Demonstration:

  • Request a non-existing URL.
  • Expect the script to redirect to the new Location.

 

4.png

 

Related document:

HTTP Commands - FortiWeb 7.6.0 documentation

Script Guide - FortiWeb documentation 

Thought Leadership. Security Summit. Thursday, November 12th, PGA National Resort, Palm Beach Gardens, FL.
Thought Leadership. Security Summit. Thursday, October 8th. Disney's Grand Californian Hotel & SPA, Anaheim, CA.
Security Operations Summit: Modernize SecOps. Operate AI-Native.AMER: November 4 | 9:00 AM PST. India and SAARC: November 5 | 10:00 AM IST. EMEA: November 5 | 10:30 AM CET. APAC: November 5 | 11:00 AM SGT