'NGINX - How to block direct access to subpages in a specific category in Wordpress and allowing access only by internal link?

I am trying to block direct access to subpages in a specific category. I have something like this in nginx site configuration file:

location ^~ /mycategory {
     valid_referers none blocked server_names;
     if ($invalid_referer) {
          return 301 https://example.com;
     }
}

But it doesn't work. I never have access to the subpage. Regardless of whether I enter the full address in the browser or use an internal link, I am always redirected to the home page. It looks like the settings with valid_referers are not working. I tried to give different variants there (example.com, https://example.com), but to no avail. Can anyone help me with this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source