Categories
apache https javascript mod-rewrite referrer

Javascript `document.referrer` vanishes when using a HTTP->HTTPS Apache redirection

I redirect all my websites from HTTP to HTTPS with:

<VirtualHost *:80>
ServerName example.com
RewriteEngine on
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
DocumentRoot /www/example.com
SSLEngine on
...
</VirtualHost>

I notice that, when navigating from a site anothersite.com and

  • clicking on a link to https://example.com, Javascript’s document.referrer works and gives anothersite.com

  • clicking on a link to http://example.com, Javascript’s document.referrer is empty!

How to prevent document.referrer to vanish when using a HTTP->HTTPS redirection via Apache?

Or should I do the automatic HTTP->HTTPS redirection with another method to keep the referrer?