TJ-
TJ-

Reputation: 14363

Caching with Tuckey UrlRewrite

I am trying to cache images, js and css by configuring the cache settings in urlrewrite.

My js/css files are getting cached as expected but the images won't. I am unable to see the Expires header for the images, despite of various combinations of configurations. Here's the configuration in urlrewrite.xml:

<rule>
    <from>^.*\.(js|css|jpeg|jpg|png)$</from>
    <set type="expires">2 years</set>
</rule>

Here's how the directories (and files) look like on the server

/html/images/foo.jpg
/html/js/bar.js
/html/css/xyz.css
/WEB-INF/urlrewrite.xml
/WEB-INF/web.xml

I have hunted for configurations everywhere I could think of and there is no specific configuration for images.

Where should I look further? Any help would be appreciated. Thank You.

PS : I have a struts/spring project.

Upvotes: 1

Views: 762

Answers (2)

shareef
shareef

Reputation: 9581

First:

You should post all your url rewrite configuration you have.

Second:

Always check documentation for examples, i dont see what have you tried.

url rewrite version 4.0

url rewrite version 3.0

Three:

Know what version your working on.

Finally:

Here is Solution i did and its working nice.

The portion i have to modify in my case is adding the both lines in three places yes three. why keep reading ))

<set type="response-header" name="Cache-Control">max-age=290304000</set>
<set type="expires" name="expires">24 hours</set>

......

    <rule>
        <from>/images/((?:(?!/).)*)/(.*)$</from>
        <to encode="true" last="true" qsappend = "true">/files/get.html&#63;file=$2&amp;preset=$1</to>
        <set type="response-header" name="Cache-Control">max-age=290304000</set>
        <set type="expires" name="expires">24 hours</set>
    </rule>

    <outbound-rule>
        <from>/images/(.*)/(.*).(jpg|png|jpeg|gif).html</from>
        <to>/images/$1/$2.$3</to>
        <set type="response-header" name="Cache-Control">max-age=290304000</set>
        <set type="expires" name="expires">24 hours</set>
    </outbound-rule>

    <rule>
        <from>/files/original/(.*)$</from>
        <to  encode="true" last="true" qsappend = "true">/files/get.html&#63;file=$1</to>
        <set type="response-header" name="Cache-Control">max-age=290304000</set>
        <set type="expires" name="expires">24 hours</set>
    </rule>

Reason:

Depending on your settings for your project there is rules and there is out bound and other stuff ; they might re write the url several times according to your rules complexity and logic so in my case tried to do it with minimum addition but, it did not work only when i add it to the three places ( without showing all my rules but you got the idea :) )

And i did this as well its Better Approach as well

Using APACHE configuration

Upvotes: 0

Radha Mohan Maheshwari
Radha Mohan Maheshwari

Reputation: 768

in which application server you are deploying tomcat? if yes

 <filter>
        <filter-name>ExpiresFilter</filter-name>
        <filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>
        <init-param>
            <param-name>ExpiresByType image</param-name>
            <param-value>access plus 100000 minutes</param-value>
        </init-param>
        <init-param>
            <param-name>ExpiresByType text/css</param-name>
            <param-value>access plus 100000 minutes</param-value>
        </init-param>
        <init-param>
            <param-name>ExpiresByType application/javascript</param-name>
            <param-value>access plus 10000 minutes</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>ExpiresFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>

use this url rewrite not required

Upvotes: 1

Related Questions