Home » URL redirection based on server date/time headers | htaccess

URL redirection based on server date/time headers | htaccess

  • by

Last Updated on May 31, 2021 by Amit

Htaccess URL redirection based on server date and time variables

Hello Apache users!
I am here again with a new
interesting article about URL rewriting.
Today , in this article I will show you how we can rewrite/redirect URLs based on several Date and Time headers.
Apache mod-rewrite has several
predefined server variables to work with date and time. If we need to redirect or rewrite our URLs on a date/time logic we can use these variables in conditions with RewriteRule.
Let’s first have a look at all the date and time variables provided by Apache mod_rewrite.

Apache Date and Time variables

  • %{TIME}
  • %{TIME_YEAR}
  • %{TIME_DAY}
  • %{TIME_MON}
  • %{TIME_HOUR}
  • %{TIME_MIN}
  • %{TIME_SEC}
  • %{TIME_WDAY}

These are all the date & time server variables provided by rewrite engine.

Variables explained

  • %{TIME} – Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) . The value of this variable looks something like 20210529073917
  • %{TIME_YEAR} – Returns the current year in YYYY format. The value of this variable looks something like 2021.
  • %{TIME_DAY} – Returns current day of the month from . Ie 29 .
  • %{TIME_MON} – Returns the current month of the year in digit format from 01 to 12 .
  • %{TIME_HOUR} – Returns the current hour in digit format from 00 to 24.
  • %{TIME_MIN} – Returns the current minute from 00 to 59 .
  • %{TIME_SEC} – Returns the current second from 00 to 59 .
  • %{TIME_WDAY} – Returns name of the current week ie “Sunday” .

URL rewriting with date & time logic

To rewrite or redirect URLs with date and time logic we can use mod-rewrite server variables . This peragraph will show you with some code examples how we can use “date and time” variables with mod_rewrite.

To redirect URLs conditionally using date and time logic here are some basic examples.
In this first example, we are writing a rule to redirect all our website URLs to example.com only if the specified condition is met ie the %{TIME_HOUR} is 05 .

RewriteEngine on
RewriteCond %{TIME_HOUR} ^05$
RewriteRule .* https://example.com [L,R]

If you have a website that is maintained on a specific day every week, say your website is maintained every Monday , and you want to automatically redirect your site URLs to a specific page /maintenance.html , you can use the following in your htaccess file :

RewriteEngine on
RewriteCond %{TIME_WDAY} ^monday$ [NC]
RewriteRule .* /maintenance.html [R,L]

The rule above will do a temporary redirection to “maintenance.html” if the specified condition is met. We are using R
instead of R=301 because we want a temporary redirection that is valid only for the current day.


If you need to redirect your site visitors to maintenance.html page every Monday on 8PM , you could use the following :

RewriteEngine on
RewriteCond %{TIME_WDAY} ^monday$ [NC]
RewriteCond %{TIME_HOUR} ^20$
RewriteRule .* /maintenance.html [R,L]

The rule above is simple and easy to understand. We have used two date time conditions to determine the exact week day and hour on which we will put the redirection.

I hope this article was helpful. If there is something that you didn’t understand or have any questions , you could post a comment below. I always reply as soon as I can.
Thanks for reading this!

Leave a Reply

Your email address will not be published. Required fields are marked *