<p>I am new to Lua, below is my inline code for &quot;EnvoyFilter&quot;. I have spent a couple of hours to search a few articles and posts. But, didn't find any suitable answer, hence posting this question.</p>
<p>The scenario is when I am getting a 503 error code I want to redirect to the login page.</p>
<pre><code>             function envoy_on_response(response_handle)
                if response_handle:headers():get(&quot;:status&quot;) == &quot;503&quot; then
                  #TODO Redirect to http://login-page.com
                end
              end
</code></pre>
<p>Any help or suggestion would be helpful and appreciated.</p>
<p>[Working Answer]</p>
<pre><code>function envoy_on_response(response_handle)
  if response_handle:headers():get(&quot;:status&quot;) == &quot;503&quot; then                
    response_handle:headers():replace(&quot;content-type&quot;, &quot;text/html&quot;)                
    response_handle:headers():replace(&quot;:status&quot;, &quot;307&quot;)                
    response_handle:headers():add(&quot;location&quot;, &quot;https://login-page.com&quot;)
  end
end 
</code></pre>

<p>You could use a <code>302 Found</code> response to instruct the users browser to make a new request to the login page.</p>
<p>Something like this should work:</p>
<p>First add some logging to validate, that it's working.
Now replace the <code>status</code> field in the header and set it to <code>302</code>.
Last add a <code>location</code> field to the header, which contains the url you want to redirect to.</p>
<pre><code>function envoy_on_response(response_handle)
    if response_handle:headers():get(&quot;:status&quot;) == &quot;503&quot; then
        response_handle:logInfo(&quot;Got status 503, redirect to login...&quot;)
        response_handle:headers():replace(&quot;:status&quot;, &quot;302&quot;)
        response_handle:headers():add(&quot;location&quot;, &quot;http://login-page.com&quot;)
    end
end
</code></pre>
<p>See docs for further inspiration: <a href="https://www.envoyproxy.io/docs/envoy/latest/configuration/http/http_filters/lua_filter" rel="nofollow noreferrer">https://www.envoyproxy.io/docs/envoy/latest/configuration/http/http_filters/lua_filter</a></p>
<p>Having said this I want to add that this might not be a good approach for failure handling. The user will not be informed about the error, but will just be redirected and land on the login page, without knowing why.</p>
<p>What problem are you trying to solve? Maybe there is a better way.</p>
