Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

2 Answers 2

up vote 2 down vote accepted

You can do that in apache using mod_rewrite like this

RewriteCond /cache/%{REQUEST_FILENAME}_%{TIME_YEAR}%{TIME_MONTH}%{TIME_DAY}_%{TIME_HOUR}.html -f
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]

from: https://craym.eu/tutoriels/referencement/variables_url_rewriting.html

share|improve this answer

Your Answer

 
discard

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.