rewrite & apache access_log

Using mod_rewrite to handle various content issues

rewrite & apache access_log

Postby broutard » Fri May 13, 2005 12:14 am

Hi,

We must use mod_rewrite but we need to analyse the apache access_log to make statistics.

Our analyser work with the old uri (with params) but when I look into the access_log file, it is the new uri which is logged (ex: www.domain.com/cart/01015/ but not www.domain.com/cart.php?id=01015)

Is anybody can tell me how to logged the rewrited uri (with params) in the access_log ?

Thanks
broutard
 
Posts: 1
Joined: Thu May 12, 2005 11:34 pm

Postby vnjug » Sat Oct 07, 2006 1:27 pm

As my understanding, you cannot. However, you can use RewriteLog (in case you are an admin) by trying to do some thing like this

httpd.conf

RewriteLog "c:/server/apache2.2/logs/rewrite.log"
RewriteLogLevel 3
vnjug
 
Posts: 6
Joined: Sat Oct 07, 2006 12:24 pm

Same issue, but rewrite_log has too much info

Postby jeffreynolds » Thu Apr 12, 2007 11:50 am

I also need to have the rewritten URL in the access_log if possible for statistics tracking. Is there some other mod or add-on that can do this?

Thanks,
Jeff
jeffreynolds
 
Posts: 5
Joined: Thu Apr 12, 2007 11:47 am
Location: Atlanta

Postby richardk » Thu Apr 12, 2007 12:21 pm

If you have access to the httpd.conf file you might be able to setup a custom log format that logs the rewritten URL if present and the request URI if it's not.

Something along the lines of (untested)
Code: Select all
# It was rewritten.
SetEnvIf rewritten_uri "^.+$" uri_to_log=%{rewritten_uri}
# It wasn't rewritten.
SetEnvIf rewritten_uri "^$" uri_to_log=%{request_uri}

LogFormat "%h %l %u %t \"%m %{uri_to_log}e %H\" %>s %b" common
CustomLog logs/access_log common


You would have to write rules like this, though. With "E=rewritten_uri:<the uri you want logged>".
Code: Select all
RewriteRule ^abc([0-9]+)$ /def-$1 [E=rewritten_uri:/def-$1,L]


http://httpd.apache.org/docs/2.2/logs.html#accesslog
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Sort of works

Postby jeffreynolds » Fri Apr 13, 2007 8:30 am

This looks as if it should work, but it doesn't.

The uri_to_log is always "%{request_uri}" literally.

It's as if the 2nd SetEnvIf always applies and instead of using the value of rewritten_uri, it literally sets uri_to_log to "%{request_uri}"

However, if I comment out the 2nd SetEnvIf, the value changes to "%{rewritten_uri}"

So that makes me think that both conditions apply and the 2nd is just overwriting the first.

Thanks,
Jeff
jeffreynolds
 
Posts: 5
Joined: Thu Apr 12, 2007 11:47 am
Location: Atlanta

Postby richardk » Fri Apr 13, 2007 10:24 am

SetEnvIf might not be able to access the variable set by mod_rewrite. This might work (again, untested)
Code: Select all
LogFormat "%h %l %u %t \"%r\" %>s %b" not_rewritten
LogFormat "%h %l %u %t \"%m %{rewritten_uri}e %H\" %>s %b" rewritten

CustomLog logs/access_log not_rewritten env=!rewritten_uri
CustomLog logs/access_log rewritten env=rewritten_uri
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

That did it

Postby jeffreynolds » Fri Apr 13, 2007 11:42 am

Thanks! That was the trick.
jeffreynolds
 
Posts: 5
Joined: Thu Apr 12, 2007 11:47 am
Location: Atlanta

When this occurs, can the other log entry be suppressed?

Postby jeffreynolds » Fri Apr 13, 2007 11:58 am

It's great to have the rewritten URI in the access_log, but is there any way to suppress the original request_uri?

Thanks,
Jeff
jeffreynolds
 
Posts: 5
Joined: Thu Apr 12, 2007 11:47 am
Location: Atlanta

Postby richardk » Sun Apr 15, 2007 10:43 am

I don't know if there's a variable that can be used to test if a request is going to be rewritten. You could try adding another variable to the mod_rewrite
Code: Select all
RewriteRule ^abc([0-9]+)$ /def-$1 [E=rewritten_uri:/def-$1,E=was_rewritten:1,L]


And then for the logs
Code: Select all
LogFormat "%h %l %u %t \"%r\" %>s %b" not_rewritten
LogFormat "%h %l %u %t \"%m %{rewritten_uri}e %H\" %>s %b" rewritten

CustomLog logs/access_log rewritten     env=was_rewritten
CustomLog logs/access_log not_rewritten env=!was_rewritten


Or if you have a limited set of rules you want this to happen to, you could try doing this
Code: Select all
# Match the URLs that you will be matching with RewriteRules.
SetEnvIf request_uri "^/uri/type/a/(.+)\.html$" will_be_rewritten
SetEnvIf request_uri "^/uri/type/b/(.+)\.gif$"  will_be_rewritten

LogFormat "%h %l %u %t \"%r\" %>s %b" not_rewritten
LogFormat "%h %l %u %t \"%m %{rewritten_uri}e %H\" %>s %b" rewritten

CustomLog logs/access_log rewritten     env=will_be_rewritten
CustomLog logs/access_log not_rewritten env=!will_be_rewritten

You will only need the "E=rewritten_uri:<the uri you want logged>" flag.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

I think Apache will still be logging everything

Postby jeffreynolds » Mon Apr 16, 2007 4:44 am

The second set of code you provided, that works for logging the rewritten URIs, creates double log entries for each of the CustomLog statements. So I left out the one that simply logged the URI that was not rewritten, since Apache logs that one already.

Now, in my access_log, I see URIs that aren't rewritten (as I normally have) and then I see URIs that are rewritten, just below the original URI. So, so that is two log entries for the same request. We're getting over 700,000 unique visits a day, so I want to try to avoid dupes as much as possible.

What I need now is a way to suppress the log entry for the URI that does get rewritten to access_log.

This latest set of code doesn't seem to address that.

Or, I could use your code as is, but I need to turn off Apache's standard logging to access_log.

Thanks again for your assistance.

- Jeff
jeffreynolds
 
Posts: 5
Joined: Thu Apr 12, 2007 11:47 am
Location: Atlanta

Next

Return to Content

Who is online

Users browsing this forum: No registered users and 15 guests

cron