Action based redirect

I’m trying to redirect programmatically based on certain actions.

It works when I query for the ‘view’ or ‘edit’ actions.

However, a query for the download or delete action doesn’t redirect as expected.

Is this intentional?

#if($xcontext.getAction() == "download")
  $response.sendRedirect($xwiki.getURL('Main.WebHome'))
#end

The download action points to an attachment, not a page. Thus that action doesn’t render the page and your script isn’t executed.

What you could probably do is implement a ResourceReferenceHandler, see https://extensions.xwiki.org/xwiki/bin/view/Extension/Resource%20API

Thanks for the clarification. I will try your suggestion then.