<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SmartLogic Solutions Blog &#187; Rack</title>
	<atom:link href="http://blog.smartlogicsolutions.com/tag/rack/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.smartlogicsolutions.com</link>
	<description>News and updates from the people at SmartLogic Solutions</description>
	<lastBuildDate>Tue, 30 Nov 2010 21:39:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Directory Conventions for Rack Middleware RubyGems</title>
		<link>http://blog.smartlogicsolutions.com/2010/05/13/directory-conventions-for-rack-middleware-rubygems/</link>
		<comments>http://blog.smartlogicsolutions.com/2010/05/13/directory-conventions-for-rack-middleware-rubygems/#comments</comments>
		<pubDate>Thu, 13 May 2010 15:48:27 +0000</pubDate>
		<dc:creator>John Trupiano</dc:creator>
				<category><![CDATA[John Trupiano]]></category>
		<category><![CDATA[Rack]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[conventions]]></category>
		<category><![CDATA[gems]]></category>
		<category><![CDATA[middleware]]></category>
		<category><![CDATA[rubygems]]></category>

		<guid isPermaLink="false">http://blog.smartlogicsolutions.com/?p=896</guid>
		<description><![CDATA[I just wanted to make a quick note about directory conventions for rack middleware gems. For all gems you should follow the convention of housing all of your code inside a single file and directory of the same name as your gem within lib, e.g. $> ls -l ~/projects/timecop/lib drwxr-xr-x 5 john staff 170 Jan [...]]]></description>
			<content:encoded><![CDATA[<p>I just wanted to make a quick note about directory conventions for rack middleware gems.  For all gems you should follow the convention of housing all of your code inside a single file and directory of the same name as your gem within lib, e.g.</p>
<p><code><br />
$> ls -l ~/projects/timecop/lib<br />
drwxr-xr-x  5 john  staff  170 Jan 14 20:31 timecop<br />
-rw-r--r--  1 john  staff   82 Jan 14 20:31 timecop.rb<br />
</code></p>
<p>The reason for this is related to how RubyGems hijacks Ruby&#8217;s require method.  When a gem is activated its lib/ folder is added to the load path.  This means that anything inside that directory is now accessible via the require method.  In order to avoid file naming collisions across gems, you must name these exactly the same as your gem. (see <a href="http://i-dont-trust-your-code.heroku.com/">slides for I Don&#8217;t Trust Your Code</a> for a more complete discussions of this)</p>
<p>However, this is slightly different with rack gems.  The convention for naming rack middleware is by using a dash, e.g. rack-rewrite.  The convention for requiring rack middleware though is to replace that dash with a slash, e.g. <code>require 'rack/rewrite'</code>.</p>
<p>The convention I&#8217;ve adopted for structuring rack middleware within a gem is to include a file by the same name as the gem and a rack directory in lib/, and then to include the second part of the middleware name as a subdirectory under that.</p>
<p><code><br />
~/projects/rack-rewrite (master) $> ls -l lib/<br />
total 8<br />
drwxr-xr-x  4 john  staff  136 Apr 17 18:02 rack<br />
-rw-r--r--@ 1 john  staff   22 Apr 17 18:02 rack-rewrite.rb</p>
<p>~/projects/rack-rewrite (master) $> ls -l lib/rack<br />
total 8<br />
drwxr-xr-x  3 john  staff  102 May 13 11:09 rewrite<br />
-rw-r--r--@ 1 john  staff  827 Apr 17 18:02 rewrite.rb<br />
</code></p>
<p>This allows my users to use either <code>require 'rack-rewrite'</code> or <code>require 'rack/rewrite'</code>.</p>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://blog.smartlogicsolutions.com/2010/05/13/rack-rewrite-1-0-0-released/" rel="bookmark" class="crp_title">Rack::Rewrite 1.0.0 Released</a></li><li><a href="http://blog.smartlogicsolutions.com/2010/01/06/rack-rewrite-0-2-1-released/" rel="bookmark" class="crp_title">Rack::Rewrite 0.2.1 Released</a></li><li><a href="http://blog.smartlogicsolutions.com/2009/11/16/rack-rewrite-for-site-maintenance-and-downtime/" rel="bookmark" class="crp_title">Rack::Rewrite for Site Maintenance and Downtime</a></li><li><a href="http://blog.smartlogicsolutions.com/2009/11/24/rack-rewrite-google-analytics-makes-site-transitions-seamless/" rel="bookmark" class="crp_title">Rack::Rewrite + Google Analytics Makes Site Transitions Seamless</a></li><li><a href="http://blog.smartlogicsolutions.com/2009/05/26/including-external-rake-files-in-your-projects-rakefile-keep-your-rake-tasks-organized/" rel="bookmark" class="crp_title">Including external .rake files in your project&#8217;s Rakefile &#8212; keep your rake tasks organized!</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://blog.smartlogicsolutions.com/2010/05/13/directory-conventions-for-rack-middleware-rubygems/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Rack::Rewrite 1.0.0 Released</title>
		<link>http://blog.smartlogicsolutions.com/2010/05/13/rack-rewrite-1-0-0-released/</link>
		<comments>http://blog.smartlogicsolutions.com/2010/05/13/rack-rewrite-1-0-0-released/#comments</comments>
		<pubDate>Thu, 13 May 2010 15:25:16 +0000</pubDate>
		<dc:creator>John Trupiano</dc:creator>
				<category><![CDATA[John Trupiano]]></category>
		<category><![CDATA[Rack]]></category>
		<category><![CDATA[rack-rewrite]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[rubygem]]></category>

		<guid isPermaLink="false">http://blog.smartlogicsolutions.com/?p=893</guid>
		<description><![CDATA[Rack::Rewrite 1.0.0 has just been released. To install simply run: gem install rack-rewrite. Rack::Rewrite is a web-server agnostic rack middleware for defining and applying rewrite rules. In many cases you can get away with Rack::Rewrite instead of writing Apache mod_rewrite rules. Documentation is hosted at RubyForge. The source code is hosted at GitHub. Updates include: [...]]]></description>
			<content:encoded><![CDATA[<p>Rack::Rewrite 1.0.0 has just been released.  To install simply run: <code>gem install rack-rewrite</code>.</p>
<p>Rack::Rewrite is a web-server agnostic rack middleware for defining and applying rewrite rules. In many cases you can get away with Rack::Rewrite instead of writing Apache mod_rewrite rules.</p>
<p>Documentation is hosted at <a href="http://johntrupiano.rubyforge.org/rack-rewrite/">RubyForge</a>.  The source code is hosted at <a href="http://github.com/jtrupiano/rack-rewrite">GitHub</a>.</p>
<p>Updates include:</p>
<p><span id="more-893"></span></p>
<p><strong>API</strong></p>
<ul>
<li>Fix rack 1.1.0 / rails3 compatibility by eliminating reliance on REQUEST_URI env param.  Paths are now constructed with PATH_INFO and QUERY_STRING</li>
<li>Follow rack directory/require convention: require &#8216;rack/rewrite&#8217; instead of &#8216;rack-rewrite&#8217;</li>
<li>Include an HTML anchor tag linked to where the URL being redirected to in the body of 301&#8242;s and 302&#8242;s</li>
</ul>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://blog.smartlogicsolutions.com/2010/01/06/rack-rewrite-0-2-1-released/" rel="bookmark" class="crp_title">Rack::Rewrite 0.2.1 Released</a></li><li><a href="http://blog.smartlogicsolutions.com/2010/05/13/directory-conventions-for-rack-middleware-rubygems/" rel="bookmark" class="crp_title">Directory Conventions for Rack Middleware RubyGems</a></li><li><a href="http://blog.smartlogicsolutions.com/2009/11/16/rack-rewrite-for-site-maintenance-and-downtime/" rel="bookmark" class="crp_title">Rack::Rewrite for Site Maintenance and Downtime</a></li><li><a href="http://blog.smartlogicsolutions.com/2009/11/24/rack-rewrite-google-analytics-makes-site-transitions-seamless/" rel="bookmark" class="crp_title">Rack::Rewrite + Google Analytics Makes Site Transitions Seamless</a></li><li><a href="http://blog.smartlogicsolutions.com/2009/09/20/timecop-0-3-0-released/" rel="bookmark" class="crp_title">Timecop 0.3.0 Released</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://blog.smartlogicsolutions.com/2010/05/13/rack-rewrite-1-0-0-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rack::Rewrite 0.2.1 Released</title>
		<link>http://blog.smartlogicsolutions.com/2010/01/06/rack-rewrite-0-2-1-released/</link>
		<comments>http://blog.smartlogicsolutions.com/2010/01/06/rack-rewrite-0-2-1-released/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 22:51:29 +0000</pubDate>
		<dc:creator>John Trupiano</dc:creator>
				<category><![CDATA[John Trupiano]]></category>
		<category><![CDATA[Rack]]></category>
		<category><![CDATA[rack-rewrite]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[rubygem]]></category>

		<guid isPermaLink="false">http://blog.smartlogicsolutions.com/?p=820</guid>
		<description><![CDATA[Rack::Rewrite 0.2.1 has just been released. To install simply run: gem install rack-rewrite. Rack::Rewrite is a web-server agnostic rack middleware for defining and applying rewrite rules. In many cases you can get away with Rack::Rewrite instead of writing Apache mod_rewrite rules. Documentation is hosted at RubyForge. The source code is hosted at GitHub. Updates include: [...]]]></description>
			<content:encoded><![CDATA[<p>Rack::Rewrite 0.2.1 has just been released.  To install simply run: <code>gem install rack-rewrite</code>.</p>
<p>Rack::Rewrite is a web-server agnostic rack middleware for defining and applying rewrite rules. In many cases you can get away with Rack::Rewrite instead of writing Apache mod_rewrite rules.</p>
<p>Documentation is hosted at <a href="http://johntrupiano.rubyforge.org/rack-rewrite/">RubyForge</a>.  The source code is hosted at <a href="http://github.com/jtrupiano/rack-rewrite">GitHub</a>.</p>
<p>Updates include:</p>
<p><span id="more-820"></span></p>
<p><strong>API</strong></p>
<ul>
<li>Implement $&#038; substitution pattern (thanks to <a href="http://github.com/bhb">Ben Brinckerhoff</a>)</li>
</ul>
<p><strong>Maintenance</strong></p>
<ul>
<li>Ignore empty captures instead of failing during subsitution (thanks to <a href="http://github.com/bhb">Ben Brinckerhoff</a>)</li>
<li>Play nice with Rack::Test requests which only set PATH_INFO and not REQUEST_URI (thanks to <a href="http://github.com/docunext">@docunext</a>)</li>
<li>Use QUERY_STRING instead of QUERYSTRING as per Rack spec.  Closes Issue #1.</li>
</ul>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://blog.smartlogicsolutions.com/2010/05/13/rack-rewrite-1-0-0-released/" rel="bookmark" class="crp_title">Rack::Rewrite 1.0.0 Released</a></li><li><a href="http://blog.smartlogicsolutions.com/2010/05/13/directory-conventions-for-rack-middleware-rubygems/" rel="bookmark" class="crp_title">Directory Conventions for Rack Middleware RubyGems</a></li><li><a href="http://blog.smartlogicsolutions.com/2009/11/16/rack-rewrite-for-site-maintenance-and-downtime/" rel="bookmark" class="crp_title">Rack::Rewrite for Site Maintenance and Downtime</a></li><li><a href="http://blog.smartlogicsolutions.com/2009/11/24/rack-rewrite-google-analytics-makes-site-transitions-seamless/" rel="bookmark" class="crp_title">Rack::Rewrite + Google Analytics Makes Site Transitions Seamless</a></li><li><a href="http://blog.smartlogicsolutions.com/2009/09/20/timecop-0-3-0-released/" rel="bookmark" class="crp_title">Timecop 0.3.0 Released</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://blog.smartlogicsolutions.com/2010/01/06/rack-rewrite-0-2-1-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rack::Rewrite + Google Analytics Makes Site Transitions Seamless</title>
		<link>http://blog.smartlogicsolutions.com/2009/11/24/rack-rewrite-google-analytics-makes-site-transitions-seamless/</link>
		<comments>http://blog.smartlogicsolutions.com/2009/11/24/rack-rewrite-google-analytics-makes-site-transitions-seamless/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 14:20:20 +0000</pubDate>
		<dc:creator>John Trupiano</dc:creator>
				<category><![CDATA[analytics]]></category>
		<category><![CDATA[Google Analytics]]></category>
		<category><![CDATA[John Trupiano]]></category>
		<category><![CDATA[Rack]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[seo]]></category>
		<category><![CDATA[googleanalytics]]></category>
		<category><![CDATA[jtrupiano]]></category>
		<category><![CDATA[rack-rewrite]]></category>

		<guid isPermaLink="false">http://blog.smartlogicsolutions.com/?p=718</guid>
		<description><![CDATA[At SmartLogic we recently rebuilt our website in rails. The previous version was a MediaWiki installation with a ton of content that had garnered a decent bit of Google juice that we did not want to lose. By setting up 301 permanent redirects for the old URL&#8217;s, we can hold onto that juice. Enter Rack::Rewrite. [...]]]></description>
			<content:encoded><![CDATA[<p>At SmartLogic we recently rebuilt our <a href="http://www.smartlogicsolutions.com">website</a> in rails.  The previous version was a MediaWiki installation with a ton of content that had garnered a decent bit of Google juice that we did not want to lose.  By setting up 301 permanent redirects for the old URL&#8217;s, we can hold onto that juice.</p>
<p><span id="more-718"></span></p>
<div id="attachment_762" class="wp-caption alignright" style="width: 160px"><a href="http://blog.smartlogicsolutions.com/wp-content/uploads/2009/11/Picture-2.png"><img src="http://blog.smartlogicsolutions.com/wp-content/uploads/2009/11/Picture-2-150x150.png" alt="Google Analytics Navigation" title="Google Analytics Navigation" width="150" height="150" class="size-thumbnail wp-image-762" /></a><p class="wp-caption-text">Google Analytics Navigation</p></div>
<p>Enter Rack::Rewrite.  <a href="http://github.com/jtrupiano/rack-rewrite">Rack::Rewrite</a> is a <a href="http://rack.rubyforge.org/">Rack</a> middleware for defining and applying rewrite rules.  Though it&#8217;s not a full replacement for <a href="http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html">Apache&#8217;s mod_rewrite</a>, a great deal of rules I&#8217;ve previously written in Apache config files can be replaced by Rack::Rewrite.  Run <code>gem install rack-rewrite</code> to install the gem.</p>
<p>In order to determine which URL&#8217;s were most important to issue 301&#8242;s for, we turned to Google Analytics.  By reviewing the most popular landing pages of the past two months from our site, we were able to methodically write our redirect rules.</p>
<div id="attachment_766" class="wp-caption aligncenter" style="width: 310px"><a href="http://blog.smartlogicsolutions.com/wp-content/uploads/2009/11/Picture-33.png"><img src="http://blog.smartlogicsolutions.com/wp-content/uploads/2009/11/Picture-33-300x291.png" alt="Google Analytics | Top Landing Pages" title="Google Analytics | Top Landing Pages" width="300" height="291" class="size-medium wp-image-766" /></a><p class="wp-caption-text">Google Analytics | Top Landing Pages</p></div>
<p>Here is a subset of the associated Rack::Rewrite rules.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  config.<span style="color:#9900CC;">middleware</span>.<span style="color:#9900CC;">insert_before</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">Rack::Lock</span>, <span style="color:#6666ff; font-weight:bold;">Rack::Rewrite</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>        
    <span style="color:#008000; font-style:italic;"># original wiki smartlogicsolutions.com website</span>
    r301 <span style="color:#996600;">'/wiki/Main_Page'</span>, <span style="color:#996600;">'/'</span>
    r301 <span style="color:#996600;">'/wiki/John_Trupiano'</span>, <span style="color:#996600;">'/john'</span>
    r301 <span style="color:#006600; font-weight:bold;">%</span>r<span style="color:#006600; font-weight:bold;">&#123;</span>^<span style="color:#006600; font-weight:bold;">/</span>wiki<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span>Charity<span style="color:#006600; font-weight:bold;">|</span>Charities<span style="color:#006600; font-weight:bold;">|</span>Local_Connection<span style="color:#006600; font-weight:bold;">&#41;</span>$<span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#996600;">'/gratis-work-and-charities'</span>
    r301 <span style="color:#996600;">'/wiki/Category:Portfolio'</span>, <span style="color:#996600;">'/portfolio'</span>
    r301 <span style="color:#996600;">'/wiki/ExxonMobil_-_Brand_Asset_Center'</span>, <span style="color:#996600;">'/portfolio/exxonmobil-brand-asset-center'</span>
    r301 <span style="color:#996600;">'/wiki/In_the_News'</span>, <span style="color:#996600;">'/in-the-news'</span>
    r301 <span style="color:#996600;">'/wiki/Getting_to_our_Office'</span>, <span style="color:#996600;">'/driving-directions'</span>
    r301 <span style="color:#996600;">'/wiki/Category:Employees'</span>, <span style="color:#996600;">'/our-team'</span>
    r301 <span style="color:#996600;">'/wiki/SimNet_for_Office_2007'</span>, <span style="color:#996600;">'/portfolio/simnet-for-office-2007-and-vista'</span>
    r301 <span style="color:#996600;">'/wiki/VNC_Collaboration_Application'</span>, <span style="color:#996600;">'/portfolio/shared-desktop'</span>
    r301 <span style="color:#996600;">'/wiki/Contact_Information'</span>, <span style="color:#996600;">'/contact-us'</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This scheme is great for landing pages, but what if we had querystring information that we wanted to keep around?  This is common for tracking codes &#8212; many marketing platforms generate URL&#8217;s that embed data in the querystring for recording and tracking purposes.  We can leverage the following trick to maintain the querystring across a rewrite.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  r301 <span style="color:#006600; font-weight:bold;">%</span>r<span style="color:#006600; font-weight:bold;">&#123;</span>^<span style="color:#006600; font-weight:bold;">/</span>wiki<span style="color:#006600; font-weight:bold;">/</span>Main_Page<span style="color:#006600; font-weight:bold;">&#40;</span>\?.<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#41;</span>?$<span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#996600;">'/$1'</span></pre></div></div>

<p>Note the following:</p>
<ul>
<li>We are conditionally matching a querystring so that the rule continues to match in the absence of a querystring. </li>
<li>We are leveraging substitution patterns to reconstitute the querystring in the rewritten URL.
</ul>
<p>Many more great use cases for Rack::Rewrite are covered in the project&#8217;s <a href="http://github.com/jtrupiano/rack-rewrite">README</a>.</p>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://blog.smartlogicsolutions.com/2010/05/13/rack-rewrite-1-0-0-released/" rel="bookmark" class="crp_title">Rack::Rewrite 1.0.0 Released</a></li><li><a href="http://blog.smartlogicsolutions.com/2010/01/06/rack-rewrite-0-2-1-released/" rel="bookmark" class="crp_title">Rack::Rewrite 0.2.1 Released</a></li><li><a href="http://blog.smartlogicsolutions.com/2009/11/16/rack-rewrite-for-site-maintenance-and-downtime/" rel="bookmark" class="crp_title">Rack::Rewrite for Site Maintenance and Downtime</a></li><li><a href="http://blog.smartlogicsolutions.com/2010/05/13/directory-conventions-for-rack-middleware-rubygems/" rel="bookmark" class="crp_title">Directory Conventions for Rack Middleware RubyGems</a></li><li><a href="http://blog.smartlogicsolutions.com/2009/09/24/google-analytics-event-tracking-overview/" rel="bookmark" class="crp_title">Google Analytics Event Tracking Overview</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://blog.smartlogicsolutions.com/2009/11/24/rack-rewrite-google-analytics-makes-site-transitions-seamless/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Rack::Rewrite for Site Maintenance and Downtime</title>
		<link>http://blog.smartlogicsolutions.com/2009/11/16/rack-rewrite-for-site-maintenance-and-downtime/</link>
		<comments>http://blog.smartlogicsolutions.com/2009/11/16/rack-rewrite-for-site-maintenance-and-downtime/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 15:36:26 +0000</pubDate>
		<dc:creator>John Trupiano</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[John Trupiano]]></category>
		<category><![CDATA[Rack]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[rack-rewrite]]></category>
		<category><![CDATA[rubygem]]></category>

		<guid isPermaLink="false">http://blog.smartlogicsolutions.com/?p=740</guid>
		<description><![CDATA[Rack::Rewrite is a Rack middleware for defining and applying rewrite rules. Though it&#8217;s not a full replacement for Apache&#8217;s mod_rewrite, a great deal of rules I&#8217;ve previously written in Apache config files can be replaced by Rack::Rewrite. Run gem install rack-rewrite to install the gem. I typically leverage rewrite rules to take my sites offline [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://github.com/jtrupiano/rack-rewrite">Rack::Rewrite</a> is a <a href="http://rack.rubyforge.org/">Rack</a> middleware for defining and applying rewrite rules.  Though it&#8217;s not a full replacement for <a href="http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html">Apache&#8217;s mod_rewrite</a>, a great deal of rules I&#8217;ve previously written in Apache config files can be replaced by Rack::Rewrite.  Run <code>gem install rack-rewrite</code> to install the gem.</p>
<p>I typically leverage rewrite rules to take my sites offline for maintenance.  Most <a href="http://www.capify.org/index.php/Capistrano">capistrano</a> users will be familiar with the following Apache rewrite ruleset.<br />
<span id="more-740"></span></p>
<pre>
  RewriteCond %{REQUEST_URI} !\.(css|jpg|png)$
  RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
  RewriteCond %{SCRIPT_FILENAME} !maintenance.html
  RewriteRule ^.*$ /system/maintenance.html [L]
</pre>
<p>This ruleset matches requests for non-asset URL&#8217;s and renders a maintenance page if it exists on the filesystem.  When capistrano users run <code>cap deploy:web:disable REASON="site upgrade" UNTIL="2PM"</code> a maintenance page is placed in public/system and this ruleset begins to kick in.  Running <code>cap deploy:web:enable</code> will remove this page and the ruleset ceases to match.</p>
<p>We can replace this ruleset with the following Rack::Rewrite rule:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#008000; font-style:italic;"># Ruby 1.8.x</span>
  maintenance_file = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>RAILS_ROOT, <span style="color:#996600;">'public'</span>, <span style="color:#996600;">'system'</span>, <span style="color:#996600;">'maintenance.html'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  send_file <span style="color:#006600; font-weight:bold;">/</span>.<span style="color:#006600; font-weight:bold;">*/</span>, maintenance_file, :<span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>rack_env<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">exists</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>maintenance_file<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> rack_env<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'REQUEST_URI'</span><span style="color:#006600; font-weight:bold;">&#93;</span> !~ <span style="color:#006600; font-weight:bold;">/</span>\.<span style="color:#006600; font-weight:bold;">&#40;</span>css<span style="color:#006600; font-weight:bold;">|</span>jpg<span style="color:#006600; font-weight:bold;">|</span>png<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span>
  <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>This rewrite rule uses the <code>send_file</code> method to return the maintenance page, uses a rule guard (the :if proc) to check for the existence of the file, and accesses the rack environment directly (rack_env arg to the Proc) to check the request URI.  Due to the shortcomings of the Ruby 1.8&#8242;s regular expression library (no negative lookahead), we have to leverage the rule guard to allow assets to continue to be served (css, jpg, png).</p>
<p>Using Ruby 1.9, this rule is simpler.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#008000; font-style:italic;"># Ruby 1.9.x</span>
  maintenance_file = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>RAILS_ROOT, <span style="color:#996600;">'public'</span>, <span style="color:#996600;">'system'</span>, <span style="color:#996600;">'maintenance.html'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  send_file <span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#41;</span>$<span style="color:#006600; font-weight:bold;">&#40;</span>?<span style="color:#006600; font-weight:bold;">&lt;</span>!css<span style="color:#006600; font-weight:bold;">|</span>png<span style="color:#006600; font-weight:bold;">|</span>jpg<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span>, maintenance_file, :<span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>rack_env<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">exists</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>maintenance_file<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Users of 1.8.x can leverage <a href="http://oniguruma.rubyforge.org/">Oniguruma</a> to keep the rule simpler.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#008000; font-style:italic;"># Ruby 1.8.x + Oniguruma</span>
  maintenance_file = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>RAILS_ROOT, <span style="color:#996600;">'public'</span>, <span style="color:#996600;">'system'</span>, <span style="color:#996600;">'maintenance.html'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  send_file <span style="color:#6666ff; font-weight:bold;">Oniguruma::ORegexp</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;(.*)$(?&lt;!css|png|jpg)&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>, maintenance_file, :<span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#CC0066; font-weight:bold;">Proc</span>.<span style="color:#9900CC;">new</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>rack_env<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">exists</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>maintenance_file<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://blog.smartlogicsolutions.com/2010/01/06/rack-rewrite-0-2-1-released/" rel="bookmark" class="crp_title">Rack::Rewrite 0.2.1 Released</a></li><li><a href="http://blog.smartlogicsolutions.com/2010/05/13/rack-rewrite-1-0-0-released/" rel="bookmark" class="crp_title">Rack::Rewrite 1.0.0 Released</a></li><li><a href="http://blog.smartlogicsolutions.com/2009/11/24/rack-rewrite-google-analytics-makes-site-transitions-seamless/" rel="bookmark" class="crp_title">Rack::Rewrite + Google Analytics Makes Site Transitions Seamless</a></li><li><a href="http://blog.smartlogicsolutions.com/2010/05/13/directory-conventions-for-rack-middleware-rubygems/" rel="bookmark" class="crp_title">Directory Conventions for Rack Middleware RubyGems</a></li><li><a href="http://blog.smartlogicsolutions.com/2008/06/02/better-setup-for-environments-in-rails/" rel="bookmark" class="crp_title">Better setup for environments in Rails</a></li><li>Powered by <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/">Contextual Related Posts</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://blog.smartlogicsolutions.com/2009/11/16/rack-rewrite-for-site-maintenance-and-downtime/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

