Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to do some cache mechanism where I create cache files in php that have names like: test_page_20110122_23.html (test_page_[php date('Ymd_G)].html)

The plan is to serve these files without php, strictly by using only apache (or later nginx). But in order to do that I have to write a rewrite rule that has the current date and time variables, eg:

RewriteCond /cache/%{REQUEST_FILENAME} _ {Ymd}_{G}.html -f
RewriteRule ^(.*)$ /cache/$1_{Ymd}_{G}.html [L]

The only problem that I have is how to get these time variables in .htacces

share|improve this question
up vote 3 down vote accepted

You can do that in apache using mod_rewrite like this

RewriteRule ^(.*)$ /cache/$1_%{TIME_YEAR}%{TIME_MONTH}%{TIME_DAY}_%{TIME_HOUR}.html [L]
share|improve this answer
works great , thanks! – SlasherZ Jan 29 '11 at 13:05

In addition to lynxman's answer:

In case %{TIME_MONTH} doesn't work try with %{TIME_MON}:

RewriteCond /cache/%{REQUEST_FILENAME}_%{TIME_YEAR}%{TIME_MON}%{TIME_DAY}_%{TIME_HOUR}.html -f
RewriteRule ^(.*)$ /cache/$1_%{TIME_YEAR}%{TIME_MON}%{TIME_DAY}_%{TIME_HOUR}.html [L]


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.