What is the correct method to log the original domain in the apache log files after a redirect 301?

Posted on

What is the correct method to log the original domain in the apache log files after a redirect 301? – Managing your servers can streamline the performance of your team by allowing them to complete complex tasks faster. Plus, it can enable them to detect problems early on before they get out of hand and compromise your business. As a result, the risk of experiencing operational setbacks is drastically lower.

But the only way to make the most of your server management is to perform it correctly. And to help you do so, this article will share nine tips on improving your server management and fix some problem about apache-2.2, logging, 301-redirect, , .

Thanks for reading my question.

I’m using Debian5 + apache2
In apache2.conf I have multiple VirtualHost sections.
I have a main domain www.mymain.com in a virtual host section and multiple other domains in another virtual host section.
This later has a redirect command as follows
redirect 301 / http://www.mymain.com

All redirects work fine.
I would like to log the original domain used but have tried every format string at http://httpd.apache.org/docs/2.3/mod/mod_log_config.html#formats to no avail.

I have used all the standard logs and my own custom (all in) log for both VirtualHost sections and the required data doesn’t appear anywhere in any of the log files.

ie
user clicks link to www.myotherdomain.com
they are successfully 301 redirected by apache to www.mymain.com

i’d like to see
blah blah www.myotherdomain.com blah blah
in the log

any input appreciated.

TIPS for folks with similar issue.

Most browsers cache 301 redirects. A brute force solution is to test using a portable install of Firefox (for example). Do tests and then delete and reinstall the application.
Or just copy a clone of the original install anytime you want to do a test.

beware using *.domain.tld in serveralias commands in apache2.conf

UseCanonicalName Off applies even if all your servernames are virtual

remember that a 301 redirect generates 2 entirely separate log entries because it’s 2 entirely separate events ie

browser requests www.myolddomain.tld -> server A returns 301 "command"
and
browser requests www.newdomain.tld (as listed in 301 cache) -> server B returns result

in theory server B may have no knowledge that a 301 occurred at all

Of course if server A and B are the same it would be very useful to be able to add the info to server B’s logs.

Solution :

It sounds like you want to use a CustomLog format (for the virtual host that sends the redirect) that includes %V, and to make sure that you’ve set UseCanonicalName Off.

Log format documentation is here.

Leave a Reply

Your email address will not be published. Required fields are marked *