How to log the URL scheme (http / https) in Apache?

One way to do it is to have two conditional CustomLog directives, controlled by whether the HTTPS variable is set.

CustomLog logs/access.log "https://..." env=HTTPS
CustomLog logs/access.log "http://..." env=!HTTPS

I have also tried using SetEnvIf in the following manner, but it doesn't work (it logs -):

SetEnv URL_SCHEME=http
SetEnvIf HTTPS on URL_SCHEME=https
CustomLog logs/access.log "%{URL_SCHEME}e://..."

For some reason I could not get the examples above to work, so found another way: you can add 2 rewrite rules into your configuration as follows:

RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ - [E=SCHEME:HTTP]

RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ - [E=SCHEME:HTTPS]

Then add this into your LogFormat definition.

scheme=\"%{SCHEME}e\"

As the url scheme is not directly available in apache log format, you may log the canonical port (e.g. 80/443) of the server serving the request by using %p as an alternative:

CustomLog access.log "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\" port:%p"