简体   繁体   中英

Htaccess: Trigger php or write to log

I want a redirect which is as fast as possible. So I decided to use htaccess redirect, because it responses even before the php interpreter is initialized. But I want to log the redirects and write something to the Database.

I tried to redirect and call rewritemap just to trigger a php file but, it only throws a 500 error.

I would be ok, if i can create a log file, even if the log processing would be delayed. Important is only: Fast redirect, track / log a redirect.

Have you got any ideas or recommendations on this?

thank you in upfront

all the best,

emre

You could use RewriteLog to log rewriting actions to a file -- that would be done by Apache, without invoking PHP.

=> Quite fast; but logs only to a file, not a database; still, as you said, the log processing can be delayed, and done by a script run from the crontab.


See also RewriteLogLevel , to configure how verbose that log should be.

Can be used in apache/vhost config, but not in .htaccess (so if you can put it there, remember to reload apache):

    RewriteEngine On
    RewriteRule /foo http://www.example.com [R=301,E=redirected]

    CustomLog /path/to/log combined env=redirected

'combined' is a default log format, but you can define your own

If your goal is simply fast as possible with logging, then the primary point of concern is keeping disk I/O to a minimum. Relying on .htaccess, you're doing a directory scan at each level of the URL (http://muffinresearch.co.uk/archives/2008/04/07/avoiding-the-use-of-htaccess-for-performance/).

If you could setup your RewriteRule in Apache's conf, and point your redirects to a PHP file, then you could have PHP running w/ APC and have the logging done to a memcache object. That way your entire client's access could occur purely in fast-access memory, and you could have a cron-job that'd routinely take the data from memcache and push it to the database (this way you'd still have long-term storage but the client's access should never require the disk to be read.)

Obviously if you're flexible on the database, you could use a Couchbase-style solution that'd essentially let you have speed of writing to memcache without storing the information in volatile memory, but I'm guessing you're locked into the database you're currently using.

You might want t try an apache log to mysql module like: http://www.outoforder.cc/projects/apache/mod_log_sql/ here is a howto for debian http://www.howtoforge.com/apache2-logging-to-a-mysql-database-with-mod_log_sql-on-debian-etch

However I'm not 100% sure this works with the latest apache.

Good Luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM