<?xml version="1.0" encoding="utf-8" ?>

<rss version="2.0" 
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:admin="http://webns.net/mvcb/"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
   xmlns:wfw="http://wellformedweb.org/CommentAPI/"
   xmlns:content="http://purl.org/rss/1.0/modules/content/"
   >
<channel>
    <title>Code in the hole (Entries tagged as mod_rewrite)</title>
    <link>http://codeinthehole.com/</link>
    <description>David Winterbottom</description>
    <dc:language>en</dc:language>
    <generator>Serendipity 1.3.1 - http://www.s9y.org/</generator>
    
    

<item>
    <title>Date-conditional redirects with mod_rewrite</title>
    <link>http://codeinthehole.com/archives/4-Date-conditional-redirects-with-mod_rewrite.html</link>
            <category>Tidbits</category>
    
    <comments>http://codeinthehole.com/archives/4-Date-conditional-redirects-with-mod_rewrite.html#comments</comments>
    <wfw:comment>http://codeinthehole.com/wfwcomment.php?cid=4</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://codeinthehole.com/rss.php?version=2.0&amp;type=comments&amp;cid=4</wfw:commentRss>
    

    <author>nospam@example.com (David Winterbottom)</author>
    <content:encoded>
    &lt;p&gt;The Apache module mod_rewite is capable of some very cool stuff.  One neat trick is to use the time and date variables to control redirects and URL rewriting.  This is useful if you have a URL that you don&#039;t want to be exposed to the world until a certain date has passed - this could be the case with special offers and competitions which have a one-off static page that isn&#039;t to be revealed until a specified date.&lt;/p&gt;
&lt;p&gt;
For example, the following directives specify that a temporary 302 redirect should be issued for all requests to the URL /special_offer before a certain date has passed: &lt;/p&gt;
&lt;div class=&quot;apache&quot; style=&quot;text-align: left&quot;&gt;&lt;span style=&quot;color: #00007f;&quot;&gt;RewriteCond&lt;/span&gt; %&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;TIME&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt; &amp;lt;&lt;span style=&quot;color: #ff0000;&quot;&gt;20081031000000&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #00007f;&quot;&gt;RewriteCond&lt;/span&gt; %&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;REQUEST_URI&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt; /special_offer.* &lt;br /&gt;&lt;span style=&quot;color: #00007f;&quot;&gt;RewriteRule&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;.*&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; /offers &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;L,R=&lt;span style=&quot;color: #ff0000;&quot;&gt;302&lt;/span&gt;,QSA&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;
This means that the /special_offer page can be prepared and deployed beforehand and apache will handle the transfer once the publish date has passed.  There&#039;s &lt;a onclick=&quot;javascript: pageTracker._trackPageview(&#039;/extlink/www.askapache.com/htaccess/mod_rewrite-tips-and-tricks.html&#039;);&quot;  href=&quot;http://www.askapache.com/htaccess/mod_rewrite-tips-and-tricks.html&quot; title=&quot;Lots of other cool mod_rewrite tricks&quot;&gt;a whole load of other nifty things that can be done in a similar vein&lt;/a&gt; such as returning different style sheets depending on the time of day.  &lt;/p&gt;
&lt;p&gt;
Digressing briefly, debugging Apache directives can be quite tricky when you&#039;re playing around trying to get something to work.  I find the easiest thing to do is to redirect to a URL which contains the variable being tested against. For example: &lt;/p&gt;
&lt;div class=&quot;apache&quot; style=&quot;text-align: left&quot;&gt;&lt;span style=&quot;color: #00007f;&quot;&gt;RewriteEngine&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;On&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #00007f;&quot;&gt;RewriteCond&lt;/span&gt; %&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;REQUEST_URI&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt; /debug &lt;br /&gt;&lt;span style=&quot;color: #00007f;&quot;&gt;RewriteRule&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;.*&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; /debug/?%&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;TIME&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&amp;amp;%&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;TIME_DAY&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#91;&lt;/span&gt;L,R=&lt;span style=&quot;color: #ff0000;&quot;&gt;302&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;
Then you can see what the values of %{TIME} and %{TIME_DAY} are from the URL you&#039;ve been redirected to.  There are almost certainly better ways of doing this but this way is quick and it works.
&lt;/p&gt; 
    </content:encoded>

    <pubDate>Fri, 31 Oct 2008 13:48:41 +0000</pubDate>
    <guid isPermaLink="false">http://codeinthehole.com/archives/4-guid.html</guid>
    <category>apache</category>
<category>mod_rewrite</category>

</item>

</channel>
</rss>