mod-rewrite

Time based rewrites - how to handle two seperate years?


I need to do rewrites so that the pages:

http://www.example.com/page-one
http://www.example.com/page-two
http://www.example.com/page-three
http://www.example.com/page-four

are only available from 25th December 2011 to 31st January 2012

How can I achieve this? I have been playing around with TIME_MON, TIME_DAY, TIME_YEAR and I am getting confused on how it will handle two separate months, two separate years, between 25 Dec 2011 and 31 Jan 2012

RewriteCond %{TIME_YEAR} ^2011
RewriteCond %{TIME_MON} ^12
RewriteCond %{TIME_DAY} >24
RewriteRule ^/page-one/?$ /folder/page-one [PT]
RewriteRule ^/page-two/?$ /folder/page-two [PT]
RewriteRule ^/page-three/?$ /folder/page-three [PT]
RewriteRule ^/page-four/?$ /folder/page-four [PT]

RewriteCond %{TIME_YEAR} ^2012
RewriteCond %{TIME_MON} ^01
RewriteCond %{TIME_DAY} >01
RewriteRule ^/page-one/?$ /folder/page-one [PT]
RewriteRule ^/page-two/?$ /folder/page-two [PT]
RewriteRule ^/page-three/?$ /folder/page-three [PT]
RewriteRule ^/page-four/?$ /folder/page-four [PT]

Solution

  • I think this should be more readable, extensible, and it should work ;)

    By the way: first thing I've looked is the format: TIME_MON => current month (0-11)

    Thank to this site here.

    So your rules will never ever work for december ;)

    RewriteCond %{TIME_YEAR}/%{TIME_MON}/%{TIME_DAY} 2011/11/(25|26|27|28|29|30|31) [OR]
    RewriteCond %{TIME_YEAR}/%{TIME_MON}/ 2012/0/
    RewriteRule ^/page-(one|two|three)/?$ /folder/page-$1 [NC,QSA,PT]
    

    ... 3 lines instead of 14 ;)

    Tell me if it works.

    If it doesn't work: read my usual "two hints", and add the rewrite log in your question.

    Two hints:

    Please try to use the RewriteLog directive: it helps you to track down such problems:

    # Trace:
    # (!) file gets big quickly, remove in prod environments:
    RewriteLog "/web/logs/mywebsite.rewrite.log"
    RewriteLogLevel 9
    RewriteEngine On
    

    My favorite tool to check for regexp:

    http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)