Search code examples
apache.htaccess

how to compare the current time to a variable in a .htaccess


I would like to make a redirection after a deadline on my site.

currently everything works correctly if I write this :

RewriteEngine On

SetEnv DEADLINE 16/06/2022_19h30

RewriteCond %{TIME_DAY}/%{TIME_MON}/%{TIME_YEAR}_%{TIME_HOUR}h%{TIME_MIN} >=16/06/2022_19h30
RewriteRule ^index.php$ read_only/index.html [L]

the advantage of using an environment variable is that I can use it elsewhere in a PHP file

So is there a way to test between the current time and the DEADLINE variable?


Solution

  • You can do this but you need to set your variable using SetEnvIf directive, so that it is available for evaluation in .htaccess RewriteCond:

    Define this variable first:

    SetEnvIf Host ^ DEADLINE=20220616193000
    

    Then use it in .htaccess using RewriteCond expressions:

    RewriteEngine On
    
    RewriteCond expr "%{TIME} -ge env('DEADLINE')"
    RewriteRule ^index\.php$ read_only/index.html [L,NC]
    

    Due to use of expr in RewriteCond, it will require Apache 2.4+