简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM