I have set a redirect rule in EventSubscriber. So according to rule, when I hit example.com/my-page in browser, it should redirect to example.com/success or some external url also.

But issue is(for anonymous user):

On hitting first time this url "example.com/my-page", REDIRECT SUCCESS!! But on second time it does not redirect. It remains "example.com/my-page" only.

Now If I logged in, flush all cache, then again log out and check "example.com/my-page", it again works -- REDIRECT SUCCESS!! But again after redirecting first time when accessed the same, it stops redirecting.

  • try implementing cache dependecy interface also class and include these methods RedirectSubscriber implements EventSubscriberInterface ,CacheableDependencyInterface { public function getCacheMaxAge() { return 0; } public function getCacheContexts() { return []; } public function getCacheTags() { return []; } – Shreya Shetty Sep 28 '16 at 6:17
  • @Shreya, I have used use Drupal\Core\Cache\CacheableMetadata; and put my request uri in context $this->context->fromRequest($request); and also I have tried $response->addCacheableDependency(CacheableMetadata::createFromRenderArray([])->addCacheTags(['rendered'])); Can you help me more figuring out the root cause of this issue? – Sugandh Khanna Sep 28 '16 at 6:34
  • did u try wht i said ?? it is not working ?? – Shreya Shetty Sep 28 '16 at 6:42
  • No Shreya, I tried exactly same what you have written, but it is redirecting only for authenticated users not anonymous users. – Sugandh Khanna Sep 28 '16 at 6:49

This is probably because of the page cache for anonymous users. The cache is implemented as middleware and runs before any event subscriber, even if you set a high priority.

If your site serves dynamically generated responses and redirects for every single request, then you don't need this cache and you can uninstall the module Internal Page Cache.

If you want to disable the page cache in code:


This will deny any page caching on the current request.

  • Is there any other alternative If I don't wanna install Internal Page cache? Can I achieve this pro-grammatically rather than manually disable module for Internal page cache? – Sugandh Khanna Sep 29 '16 at 6:53
  • 1
    Yes, this is possible, see the edited answer. – 4k4 Sep 29 '16 at 7:06
  • Should I use it when saving form to database or at event subscriber where redirecting rule is written? – Sugandh Khanna Sep 29 '16 at 7:08
  • The kill switch can be triggered anywhere in code of the same request. However if you set a redirect in a form to /example.com/my-page, then you get a second request, and a kill switch triggered in the form will no longer be active. – 4k4 Sep 29 '16 at 7:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.