<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Efficient Caching With mod_rewrite</title>
	<atom:link href="http://www.barneyb.com/barneyblog/2009/05/19/efficient-caching-with-mod-rewrite/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.barneyb.com/barneyblog/2009/05/19/efficient-caching-with-mod-rewrite/</link>
	<description>Thoughts, rants, and even some code from the mind of Barney Boisvert.</description>
	<lastBuildDate>Thu, 11 Sep 2014 09:58:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: barneyb</title>
		<link>https://www.barneyb.com/barneyblog/2009/05/19/efficient-caching-with-mod-rewrite/comment-page-1/#comment-284354</link>
		<dc:creator>barneyb</dc:creator>
		<pubDate>Thu, 17 Nov 2011 18:12:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/?p=969#comment-284354</guid>
		<description>Matthias, I&#039;m not sure what your rule is supposed to be doing (it got munged), but it&#039;s certainly not going to to do caching or compressing.  The &#039;cachelifetime&#039; is going to set the cache headers on the REDIRECT response, not the destination resource.  When it gets to the end of the redirect, that&#039;s an entirely new request, and it&#039;ll have a completely unrelated set of cache headers.  What you want is something like this:

&lt;pre&gt;# this does cache related stuff
&lt;Location ~ &quot;^/path/to/assets/.*\.(js&#124;css&#124;png)$&quot;&gt;
    ExpiresActive   On
    ExpiresByType   application/x-javascript    &quot;now plus 1 month&quot;
    ExpiresByType   text/css                    &quot;now plus 1 month&quot;
    ExpiresByType   image/png                   &quot;now plus 1 month&quot;

    Header  set     Cache-Control       &quot;public&quot;
    Header  set     Pragma              &quot;&quot;
    Header  unset   Last-Modified
    Header  unset   ETag
    FileETag    None
&lt;/Location&gt;

# this enabled deflating (gzipping) JS and CSS
&lt;FilesMatch &quot;\.(js&#124;css)$&quot;&gt;
    SetOutputFilter DEFLATE
&lt;/FilesMatch&gt;
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Matthias, I'm not sure what your rule is supposed to be doing (it got munged), but it's certainly not going to to do caching or compressing.  The 'cachelifetime' is going to set the cache headers on the REDIRECT response, not the destination resource.  When it gets to the end of the redirect, that's an entirely new request, and it'll have a completely unrelated set of cache headers.  What you want is something like this:</p>
<pre># this does cache related stuff
&lt;Location ~ "^/path/to/assets/.*\.(js|css|png)$"&gt;
    ExpiresActive   On
    ExpiresByType   application/x-javascript    "now plus 1 month"
    ExpiresByType   text/css                    "now plus 1 month"
    ExpiresByType   image/png                   "now plus 1 month"

    Header  set     Cache-Control       "public"
    Header  set     Pragma              ""
    Header  unset   Last-Modified
    Header  unset   ETag
    FileETag    None
&lt;/Location&gt;

# this enabled deflating (gzipping) JS and CSS
&lt;FilesMatch "\.(js|css)$"&gt;
    SetOutputFilter DEFLATE
&lt;/FilesMatch&gt;
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matthias</title>
		<link>https://www.barneyb.com/barneyblog/2009/05/19/efficient-caching-with-mod-rewrite/comment-page-1/#comment-284243</link>
		<dc:creator>Matthias</dc:creator>
		<pubDate>Wed, 16 Nov 2011 10:11:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/?p=969#comment-284243</guid>
		<description>The Script that I tried is:

RewriteRule /

[redirect=permanent,cachelifetime=&quot;1 month&quot;] 

But it doesn`t work!!!!</description>
		<content:encoded><![CDATA[<p>The Script that I tried is:</p>
<p>RewriteRule /</p>
<p>[redirect=permanent,cachelifetime="1 month"] </p>
<p>But it doesn`t work!!!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matthias Luther</title>
		<link>https://www.barneyb.com/barneyblog/2009/05/19/efficient-caching-with-mod-rewrite/comment-page-1/#comment-284242</link>
		<dc:creator>Matthias Luther</dc:creator>
		<pubDate>Wed, 16 Nov 2011 10:08:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/?p=969#comment-284242</guid>
		<description>Hi,

I try to realize a script  witch Rewrite to set the caching of images to an month and to compress the js-scripts. It is for a static html-page. 

The problem is I have no idea how to manage that! Is it possible in a .htaccess?

Greeds &amp; Thanks,
Matthias</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I try to realize a script  witch Rewrite to set the caching of images to an month and to compress the js-scripts. It is for a static html-page. </p>
<p>The problem is I have no idea how to manage that! Is it possible in a .htaccess?</p>
<p>Greeds &amp; Thanks,<br />
Matthias</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: barneyb</title>
		<link>https://www.barneyb.com/barneyblog/2009/05/19/efficient-caching-with-mod-rewrite/comment-page-1/#comment-178553</link>
		<dc:creator>barneyb</dc:creator>
		<pubDate>Thu, 21 May 2009 21:42:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/?p=969#comment-178553</guid>
		<description>Henry,

I have no idea.  As long as it has the &quot;check for file existence&quot; capabilities, I don&#039;t see why it wouldn&#039;t work, but I don&#039;t run IIS anywhere.</description>
		<content:encoded><![CDATA[<p>Henry,</p>
<p>I have no idea.  As long as it has the "check for file existence" capabilities, I don't see why it wouldn't work, but I don't run IIS anywhere.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Henry Ho</title>
		<link>https://www.barneyb.com/barneyblog/2009/05/19/efficient-caching-with-mod-rewrite/comment-page-1/#comment-178549</link>
		<dc:creator>Henry Ho</dc:creator>
		<pubDate>Thu, 21 May 2009 21:32:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/?p=969#comment-178549</guid>
		<description>hmm... does this work with URL Rewrite on IIS7?</description>
		<content:encoded><![CDATA[<p>hmm&#8230; does this work with URL Rewrite on IIS7?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jamie Krug</title>
		<link>https://www.barneyb.com/barneyblog/2009/05/19/efficient-caching-with-mod-rewrite/comment-page-1/#comment-178428</link>
		<dc:creator>Jamie Krug</dc:creator>
		<pubDate>Wed, 20 May 2009 23:57:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/?p=969#comment-178428</guid>
		<description>Wow, Apache is smart :) That&#039;s great info, and the WordPress MU sample is extremely helpful. Thanks!</description>
		<content:encoded><![CDATA[<p>Wow, Apache is smart :) That's great info, and the WordPress MU sample is extremely helpful. Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: barneyb</title>
		<link>https://www.barneyb.com/barneyblog/2009/05/19/efficient-caching-with-mod-rewrite/comment-page-1/#comment-178425</link>
		<dc:creator>barneyb</dc:creator>
		<pubDate>Wed, 20 May 2009 23:44:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/?p=969#comment-178425</guid>
		<description>Apache&#039;s even smarter than that.  It only does the RewriteCond directives if the RewriteRule matches.  So the order of operations is check the RewriteRule, ensure all RewriteConds pass, do the rewrite.  A request to &quot;/mypage.html&quot; won&#039;t match the RewriteRule, so it&#039;ll fall through right then.  A request to &quot;/tn/p1234-100x100.jpg&quot; that doesn&#039;t exist will match the RewriteRule, but will fail on the RewriteCond (after that filesystem check).

Bottom line, disk I/O is the least of your worries.  Doing some filesystem checks is going to be WAY faster than spinning up an HttpRequest for CF to process, let alone the actual CF processing.  If you&#039;ve got dedicated servers for purely static assets, then the I/O will become a potential issue, but short of that it&#039;s quite unlikely.  Any decent OS will cache in RAM as well, which will quite possible avoid even a disk seek.

WordPress MU, for example, uses that exact sort of global rewrite with existence check:

&lt;pre&gt;# if it&#039;s a real path, just serve it
RewriteCond  %{REQUEST_FILENAME}  -f  [OR]
RewriteCond  %{REQUEST_FILENAME}  -d
RewriteRule  .                    -   [L]
# strip the blog slug and run wp- and .php resources
RewriteRule  ^([_0-9a-zA-Z-]+/)?(wp-.*)     $2  [L]
RewriteRule  ^([_0-9a-zA-Z-]+/)?(.*\.php)$  $2  [L]
# point everything else at index.php
RewriteRule  .  index.php  [L]
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Apache's even smarter than that.  It only does the RewriteCond directives if the RewriteRule matches.  So the order of operations is check the RewriteRule, ensure all RewriteConds pass, do the rewrite.  A request to "/mypage.html" won't match the RewriteRule, so it'll fall through right then.  A request to "/tn/p1234-100&#215;100.jpg" that doesn't exist will match the RewriteRule, but will fail on the RewriteCond (after that filesystem check).</p>
<p>Bottom line, disk I/O is the least of your worries.  Doing some filesystem checks is going to be WAY faster than spinning up an HttpRequest for CF to process, let alone the actual CF processing.  If you've got dedicated servers for purely static assets, then the I/O will become a potential issue, but short of that it's quite unlikely.  Any decent OS will cache in RAM as well, which will quite possible avoid even a disk seek.</p>
<p>WordPress MU, for example, uses that exact sort of global rewrite with existence check:</p>
<pre># if it's a real path, just serve it
RewriteCond  %{REQUEST_FILENAME}  -f  [OR]
RewriteCond  %{REQUEST_FILENAME}  -d
RewriteRule  .                    -   [L]
# strip the blog slug and run wp- and .php resources
RewriteRule  ^([_0-9a-zA-Z-]+/)?(wp-.*)     $2  [L]
RewriteRule  ^([_0-9a-zA-Z-]+/)?(.*\.php)$  $2  [L]
# point everything else at index.php
RewriteRule  .  index.php  [L]
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jamie Krug</title>
		<link>https://www.barneyb.com/barneyblog/2009/05/19/efficient-caching-with-mod-rewrite/comment-page-1/#comment-178423</link>
		<dc:creator>Jamie Krug</dc:creator>
		<pubDate>Wed, 20 May 2009 23:35:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/?p=969#comment-178423</guid>
		<description>Barney,

Dude, I apologize -- my first comment is literally half the length of your post! Here&#039;s a quick follow up...

Along the lines of my prior comment -- I&#039;m assuming Apache is smart enough to stop testing chained RewriteCond if one fails (since adjacent RewriteCond lines are chained by implicit AND&#039;s), so in your example I think you could save those microseconds on the disk hit that I&#039;m worried about ;-) You could add this first RewriteCond prior to what you show in the example... I think?

&lt;pre&gt;RewriteCond  %{REQUEST_URI}  ^/tn/p([0-9]+)-([0-9]+)x([0-9]+)\.jpg$
RewriteCond  %{REQUEST_FILENAME}  !-s
RewriteRule  ^/tn/p([0-9]+)-([0-9]+)x([0-9]+)\.jpg$  /gen_tn.cfm?id=$1&amp;width=$2&amp;height=$3  [PT,L]
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Barney,</p>
<p>Dude, I apologize &#8212; my first comment is literally half the length of your post! Here's a quick follow up&#8230;</p>
<p>Along the lines of my prior comment &#8212; I'm assuming Apache is smart enough to stop testing chained RewriteCond if one fails (since adjacent RewriteCond lines are chained by implicit AND's), so in your example I think you could save those microseconds on the disk hit that I'm worried about ;-) You could add this first RewriteCond prior to what you show in the example&#8230; I think?</p>
<pre>RewriteCond  %{REQUEST_URI}  ^/tn/p([0-9]+)-([0-9]+)x([0-9]+)\.jpg$
RewriteCond  %{REQUEST_FILENAME}  !-s
RewriteRule  ^/tn/p([0-9]+)-([0-9]+)x([0-9]+)\.jpg$  /gen_tn.cfm?id=$1&amp;width=$2&amp;height=$3  [PT,L]
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jamie Krug</title>
		<link>https://www.barneyb.com/barneyblog/2009/05/19/efficient-caching-with-mod-rewrite/comment-page-1/#comment-178409</link>
		<dc:creator>Jamie Krug</dc:creator>
		<pubDate>Wed, 20 May 2009 20:31:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.barneyb.com/barneyblog/?p=969#comment-178409</guid>
		<description>Hey, Barney, this is really interesting stuff -- keep it coming!

In fact, I was working on some mod_rewrite stuff just this morning, which is slightly related. I was using the !-s in a RewriteCond for a slightly different purpose, but I became concerned that this could actually worsen performance in my case. I assume &quot;RewriteCond %{REQUEST_FILENAME} !-s&quot; must hit the file system every time, since it&#039;s checking for the existence of a file on disk and its size, so there must be some penalty there, right?

In your case, I&#039;d imagine it&#039;s probably worthwhile, because that&#039;s certainly faster than regenerating an image every time, however, that condition check occurs on *every* other request (unless you can use other RewriteCond/RewriteRule logic to &quot;short-circuit&quot; when the !-s check is not required).

Here&#039;s my situation: I&#039;m simply using a rewrite rule for a CMS so I can avoid /index.cfm/alias-stuff/here/ type URLs and rewrite something like /alias-stuff/here/ instead. However, there are other *.cfm URLs on the site that I do not want to rewrite. So, here was my first pass, which works great:

&lt;pre&gt;RewriteCond  %{DOCUMENT_ROOT}/%{REQUEST_FILENAME}  !-d
RewriteCond  %{DOCUMENT_ROOT}/%{REQUEST_FILENAME}  !-s
RewriteRule  ^(.*)$  /index.cfm%{REQUEST_URI}  [NE,QSA]
&lt;/pre&gt;

When I became paranoid about the disk hits, I changed it to the following, which has one RewriteCond that will be met as long as there is a period (.) in the REQUEST_URI (because my CMS-aliased URLs will never contain a period, but any URL with a file exension will):

&lt;pre&gt;RewriteCond  %{REQUEST_URI}  !^(.*\..+)
RewriteRule  ^(.*)$  /index.cfm%{REQUEST_URI}  [NE,QSA]
&lt;/pre&gt;

This seems to work great for my current situation, but isn&#039;t quite as universally reusable as the first option. My thinking is that the one condition with a regex evaluation will perform better than 2 disk hits, which would occur on every request, even the many requests for images, CSS, JS, etc. Am I way too paranoid, or does this make sense to you?

One other option I considered was a condition that ensured specific extensions were not in the URL, but it seemed unruly and more difficult to maintain:

&lt;pre&gt;RewriteCond  %{REQUEST_URI}  !^(.*\.(avi&#124;bmp&#124;cfc&#124;cfm&#124;cfml&#124;cfr&#124;css&#124;csv&#124;doc&#124;gif&#124;htm&#124;html&#124;jpg&#124;js&#124;mov&#124;mp3&#124;mpeg&#124;php&#124;pdf&#124;png&#124;ppt&#124;swf&#124;txt&#124;xls))
&lt;/pre&gt;

Finally (sorry this is such a long-winded comment!)... You may have noticed that I used this:

&lt;pre&gt;RewriteCond  %{DOCUMENT_ROOT}/%{REQUEST_FILENAME}  !-s
&lt;/pre&gt;

...instead of this:

&lt;pre&gt;RewriteCond  %{REQUEST_FILENAME}  !-s
&lt;/pre&gt;

Everything I&#039;ve read, including your example, suggests that %{REQUEST_FILENAME} would represent the full physical path, but I had to include &quot;%{DOCUMENT_ROOT}/&quot; in front to get it to work. Do you know of another Apache configuration that would cause this issue? FWIW, I&#039;m doing all of my rewrite rules inside my vhosts, not .htaccess.

Thanks!</description>
		<content:encoded><![CDATA[<p>Hey, Barney, this is really interesting stuff &#8212; keep it coming!</p>
<p>In fact, I was working on some mod_rewrite stuff just this morning, which is slightly related. I was using the !-s in a RewriteCond for a slightly different purpose, but I became concerned that this could actually worsen performance in my case. I assume "RewriteCond %{REQUEST_FILENAME} !-s" must hit the file system every time, since it's checking for the existence of a file on disk and its size, so there must be some penalty there, right?</p>
<p>In your case, I'd imagine it's probably worthwhile, because that's certainly faster than regenerating an image every time, however, that condition check occurs on *every* other request (unless you can use other RewriteCond/RewriteRule logic to "short-circuit" when the !-s check is not required).</p>
<p>Here's my situation: I'm simply using a rewrite rule for a CMS so I can avoid /index.cfm/alias-stuff/here/ type URLs and rewrite something like /alias-stuff/here/ instead. However, there are other *.cfm URLs on the site that I do not want to rewrite. So, here was my first pass, which works great:</p>
<pre>RewriteCond  %{DOCUMENT_ROOT}/%{REQUEST_FILENAME}  !-d
RewriteCond  %{DOCUMENT_ROOT}/%{REQUEST_FILENAME}  !-s
RewriteRule  ^(.*)$  /index.cfm%{REQUEST_URI}  [NE,QSA]
</pre>
<p>When I became paranoid about the disk hits, I changed it to the following, which has one RewriteCond that will be met as long as there is a period (.) in the REQUEST_URI (because my CMS-aliased URLs will never contain a period, but any URL with a file exension will):</p>
<pre>RewriteCond  %{REQUEST_URI}  !^(.*\..+)
RewriteRule  ^(.*)$  /index.cfm%{REQUEST_URI}  [NE,QSA]
</pre>
<p>This seems to work great for my current situation, but isn't quite as universally reusable as the first option. My thinking is that the one condition with a regex evaluation will perform better than 2 disk hits, which would occur on every request, even the many requests for images, CSS, JS, etc. Am I way too paranoid, or does this make sense to you?</p>
<p>One other option I considered was a condition that ensured specific extensions were not in the URL, but it seemed unruly and more difficult to maintain:</p>
<pre>RewriteCond  %{REQUEST_URI}  !^(.*\.(avi|bmp|cfc|cfm|cfml|cfr|css|csv|doc|gif|htm|html|jpg|js|mov|mp3|mpeg|php|pdf|png|ppt|swf|txt|xls))
</pre>
<p>Finally (sorry this is such a long-winded comment!)&#8230; You may have noticed that I used this:</p>
<pre>RewriteCond  %{DOCUMENT_ROOT}/%{REQUEST_FILENAME}  !-s
</pre>
<p>&#8230;instead of this:</p>
<pre>RewriteCond  %{REQUEST_FILENAME}  !-s
</pre>
<p>Everything I've read, including your example, suggests that %{REQUEST_FILENAME} would represent the full physical path, but I had to include "%{DOCUMENT_ROOT}/" in front to get it to work. Do you know of another Apache configuration that would cause this issue? FWIW, I'm doing all of my rewrite rules inside my vhosts, not .htaccess.</p>
<p>Thanks!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
