in Apache

.htaccess redirects based on date and time

A useful trick for implementing maintenance windows and redirects without having to use a php or similar script is to check date and time in .htaccess files or use it to build a redirect url.

Date and time values in .htaccess come in the form %{TIME_XXXX} where XXXX is the type of date or time you want.

So if you want to redirect a generic url to one which contains today’s date, you might use:

RewriteRule ^posts/today$ /posts/%{TIME_YEAR}-%{TIME_MON}-%{TIME_DAY}

That would result in /posts/today being redirected to something like /posts/2015-08-27

If you wanted redirect a page after a date (and time) is passed you could use something like the following, where if the date and time is passed 9am on 27th August 2015 the redirect will happen. We use a simple number comparison of turning the date into an integer and then comparing it.

RewriteCond %{TIME_YEAR}%{TIME_MON}%{TIME_DAY}%{TIME_HOUR} >2015082709
RewriteRule ^$ /destination/url.html [R=301,L]

The following would only redirect until a specific time (10.22am on 27th August 2015)

RewriteCond %{TIME_YEAR}%{TIME_MON}%{TIME_DAY}%{TIME_HOUR}%{TIME_MIN} <201508271022
RewriteRule ^$ /destination/url.html [R=301,L]

The following would only redirect between two specific dates (20th July 2015 and 27th August 2015)

RewriteCond %{TIME_YEAR}%{TIME_MON}%{TIME_DAY} <20150828
RewriteCond %{TIME_YEAR}%{TIME_MON}%{TIME_DAY} >20150719
RewriteRule ^$ /destination/url.html [R=301,L]

The options you have for %{TIME_XXXX} values are:

TIME_YEAR // current four-digit year
TIME_MON // current month
TIME_DAY // current day of month
TIME_HOUR // current hour (24 hour clock) of day
TIME_MIN // current minute of hour
TIME_SEC // current second of minute
TIME_WDAY // current week-day
TIME // a formatted string representing the date and time down to seconds. e.g. 20150827112234

Share your thoughts


This site uses Akismet to reduce spam. Learn how your comment data is processed.

  1. Really Great Stuff…!!!
    Sir,If I want to set date-time to upcoming means its not today’s date then what should I do…?
    Example if today is may-11-2016 and I want to redirect it on
    dec-31-2016 to some url then what should I do…?

  2. Hello Harry,

    Thanks a lot for this post, although it’s not the newest one on the web, it’s still one of the best to understand time-conditionned rewriting rules!

    I have been trying to rewrite urls for my site (hosted on OVH, I use WP as CMS) with objective to serve different content based on day/time of the week. Unfortunately, I did not succeed and figured I’d write to you to see if I could get some advice/guidance.

    My aim is to redirect page 1 to page 2 on a given date and time. Sounds ridiculously simple, but yet I am failing at it!

    Page 1 is
    Page 2 is
    Redirection should be triggered on November 30 2017, at 15:00.

    Here are the few codes I tried (given date/time was today I could see it did not work!):

    Attempt 1:
    RewriteEngine On
    RewriteBase /
    RewriteCond %{TIME_YEAR}%{TIME_MON}%{TIME_DAY}%{TIME_HOUR} >2017113015
    RewriteRule ^debug$ /commander [R=301,L]

    Attempt 2:
    RewriteCond %{TIME_YEAR}%{TIME_MON}%{TIME_DAY}%{TIME_HOUR} >2017113015
    RewriteRule ^debug$ /commander.html [R=301,L]

    I tried few other stuff but after reading over and over your tuto I believe these have to be the closest 2 attempts I made.

    In all fairness, the rule I need to see applied is slightly more complicated (see below) but I guess if I can get the simple version to work, I’ll work out the more complex one!

    The redirection should only happen between 2 specific day/time of the week, every week:
    -between Sunday 05:00pm and Wednesday 05:00pm, “…/debug” is redirected to “…/commander”
    – between Wednesday 05:01pm and Sunday 04:59pm, “…/debug” is not redirected

    Thanks a lot in advance if you can help!

  3. Without trying your code myself…. are you sure your server is running on local time? Have you checked your code with a couple of hours ago as the time? What about yesterday?

  4. Hi again an thanks for your answer!

    Am sure my server runs on local time plus I did check using the below code (at bottom, uncommented) to verify what the server exactly returns, and that’s exactly right…

    I also tried with a few hours (and even days) ago as the time, but did not work either…

    I guess I’ll search for another solution… But if anything crosses your mind, I’d appreciate the tip!


    # Code to check server time
    RewriteEngine On
    RewriteBase /
    RewriteCond %{QUERY_STRING} !time [NC]
    #RewriteCond %{TIME} ^(.*)
    #RewriteCond %{TIME_YEAR} ^(.*)
    #RewriteCond %{TIME_MON} ^(.*)
    #RewriteCond %{TIME_WDAY} ^(.*)
    #RewriteCond %{TIME_DAY} ^(.*)
    #RewriteCond %{TIME_HOUR} ^(.*)
    #RewriteCond %{TIME_MIN} ^(.*)
    #RewriteCond %{TIME_SEC} ^(.*)
    RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI}?time=%1 [R,L]

  5. @Johan,

    You’ll need to do something like:

    RewriteCond %{REQUEST_URI} ^/page/
    RewriteCond %{TIME_WDAY} =0
    RewriteRule ^(.*)$ http://%{HTTP_HOST}/other-page/$1 [NC,L]

    Weekdays are numbered as Sunday = 0 to Saturday = 6.

  6. Hi,
    great post, thanks.
    Ist there any option to use/combine the ‘time variables’ with mod_substitute / mod_filter to enhance content of css and js files?
    I would like to add a random timestamp string, to change the md5sum of some static pages.

    Because to do this changes, prevent attacks with the Linux tool wpscan. So the hacker can not find out the version of installed WordPress.

  7. Hi,
    does anyone know, how to combine ‘TIME_SEC’ with mod_substitute and mod_filter?
    I need to add a timestamp inside of some js and css files to avoid WordPress version sniffing by Linux ‘wpscan’.

    This tool calculate the md5sum.

    By add a timestamp, I want to change this md5sum.

    So far, I didnt find help on Google.

    Best regards