This project is read-only.

Rewrite Rules Module and Cache Module Conflicts

Topics: General
Jan 7, 2013 at 12:29 PM

Just a little heads up about a nuance I ran into recently when the cache module and the rewrite rules module are both enabled.

I have a multi-tenant deployment and I use the rewrite rules module to perform 301 redirects from to for each tenant. I only have the rewrite rules module enabled in my default instance and define the rewrite rules for the domains for all tenants there...

RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [R=301,NC]

This avoids the need to enable the rewrite rules module for each tenant, avoids having to specify the rewrite rules separately within the admin area of each tenant and avoids having to add "" to the Host field in the tenant config for each tenant (ie. I just specify "" for the Host rather than "," which would be needed for the rewrite rules I defined above to work within each tenant).

Anyhow, recently I enabled the Cache module in my default instance (which I didn't really need to do but did anyways) and shortly after doing so I noticed that for all my tenants, if I went to in the browser (without the www.) then the home page for the default instance would be displayed, instead of being 301'd to As soon as I disabled the cache module in my default instance, the 301's worked again.

Since I didn't need caching in my default instance (as it's only me that uses it to administer the tenants) I just left the caching disabled. But out of interest, I changed my configuration for a couple of tenants to have the rewrite rules module enabled within them and perform the rewrite themselves. All of my tenant websites have caching enabled. Once I did this, I noticed that when I went to I was no longer redirected to

It seems the cached home page was served to before the redirect.

So it seems to me that when caching is enabled and the rewrite rules module is enabled, the cache will serve a cached page prior to the rewrite rules being evaluated. And it seems the cache module caches the same page for both and

I wondering would it be possible to update the cache module so that it takes into account the host in the request? This way it could cache the homepage for the request, but cache the 301 response for the request.

Otherwise, if you're using the rewrite rules module for 301 redirects from to you need to be aware that this might not work as expected if you have the cache module enabled.

You can work-around the issue as I have done, by performing the 301 redirects from the default instance that doesn't have caching enabled. Presumably a tenant without caching enabled could also be used to do the 301 redirects (you'd just have to add each non-www domain to that tenant's Host field in the tenant configuration).