codebird
codebird

Reputation: 397

How to replace timestamp placeholder in URL?

I'm looking for a way to emulate a behaviour I've seen on ad servers, for purposes of internal testing and tracking. I want to be able to check for and replace certain placeholders such as, for instance, {timestamp} in my URLs.

Say I have a URL like so: http://suchandsuchasite.com/frogs.php?x=43&thetime={timestamp}&bird=albatross. My current theory is that I should be able to add a RewriteRule to the .htaccess file to replace the string {timestamp} with the actual current timestamp. But I'm new to mod_rewrite and not all that familiar with regular expressions, so I'm making very slow progress in making this work. All I've got so far is:

RewriteCond %{QUERY_STRING} &?thetime={timestamp}$

... and I'm not even sure that that is correct. I've been looking at the Apache documentation, so I know that the next line should probably follow this syntax:

# RewriteRule *Pattern* *Substitution* [flags]

... but I'm not sure how to replace just that one variable and preserve the rest of the query string. Also, is it even possible to get the current timestamp somehow from within the .htaccess file? Should I be doing this via the PHP headers instead? Or something else completely different?

Many thanks for any assistance you might be willing to offer!

Upvotes: 0

Views: 958

Answers (1)

larsks
larsks

Reputation: 311288

Ignoring for the moment why you want to do this, you can do this via a mod_rewrite program map -- that is, a rewrite map for which the replacement string is generated by running some code. Something like this might work:

RewriteMap timemap program:/path/to/file/timestamper.py
RewriteRule {timestamp} ${timemap:NULL}

And in /path/to/timestamper.py:

#!/usr/bin/python

import sys
import time

while True:
  request = sys.stdin.readline().strip()
  response = int(time.time())
  sys.stdout.write(response + '\n')
  sys.stdout.flush()

Apache starts up timestamper.py when the webserver starts up, and then feeds it things to rewrite. In this case, Apache always sends it NULL, and the script ignores the input value and outputs a Unix timestamp.

Keep in mind that mod_rewrite processes requests (it does not filter URLs generated by your webpages/webapps/etc). As CBroe says, it's not clear that rewriting the URL at this point is what you want to do.

Upvotes: 1

Related Questions