Blog

Skipping cache for POST actions in Apache1

If you used page caching in a site that implements RESTful interface, you may face a problem. You could have multiple actions that share a single URL with different HTTP methods. For example, showing a document is served at /documents/1, while updating the same document is also at /documents/1. When I'm trying to update a document, the request is something like

PUT /documents/1

If your Apache is not well configured, it may respond with a cached file at /documents/1.html. This is wrong because this file should be served for GET requests only. In fact, POST, PUT and DELETE actions should never be cached because they change server state. Investigating in this issue, I found the following snippet at my .htaccess file.

    # Conditions for fastcgi
    RewriteRule ^$ index.html [QSA]
    RewriteRule ^([^.]+)$ $1.html [QSA]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

To solve this problem I changed a line and added a new line to make it like this

    # Conditions for fastcgi
    RewriteRule ^$ index.html [QSA]
    RewriteRule ^([^.]+)$ $1.html [QSA]
    RewriteCond %{REQUEST_FILENAME} !-f [OR]
    RewriteCond %{THE_REQUEST} !^GET

    RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

This allows a request to served from file system only when the request is a GET request. This resolved the problem.

  • 1 Comments

  • Blog_user_avatar Says:

    bounatiro

Leave a Comment...