Apache 2.4
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
DocumentRoot /var/www/www.example.com/docroot
<Directory /var/www/www.example.com/docroot>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
CustomLog /var/log/apache2/www.example.com_access.log combined
ErrorLog /var/log/apache2/www.example.com_error.log
# Force redirect to www.example.com
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
SetEnvIf X-Forwarded-Proto "^https$" HTTPS=on
</VirtualHost>
## Uncomment/configure the following VirtualHost for SSL
#<VirtualHost *:443>
# ServerName www.example.com
#
# SSLEngine on
# SSLCertificateFile /etc/ssl/certs/www.example.com.pem
# SSLCertificateKeyFile /etc/ssl/private/www.example.com.key
#
# ProxyRequests Off
# ProxyPreserveHost On
# ProxyPass / http://localhost/
# ProxyPassReverse / http://localhost/
#
# RequestHeader set X-Forwarded-Proto "https"
#
# Header add Strict-Transport-Security "max-age=15552000"
#</VirtualHost>
Apache 2.2
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
DocumentRoot /var/www/www.example.com/docroot
<Directory /var/www/www.example.com/docroot>
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
CustomLog /var/log/apache2/www.example.com_access.log combined
ErrorLog /var/log/apache2/www.example.com_error.log
# Force redirect to www.example.com
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
SetEnvIf X-Forwarded-Proto "^https$" HTTPS=on
</VirtualHost>
## Uncomment/configure the following VirtualHost for SSL
#<VirtualHost *:443>
# ServerName www.example.com
#
# SSLEngine on
# SSLCertificateFile /etc/ssl/certs/www.example.com.pem
# SSLCertificateKeyFile /etc/ssl/private/www.example.com.key
#
# ProxyRequests Off
# ProxyPreserveHost On
# ProxyPass / http://localhost/
# ProxyPassReverse / http://localhost/
#
# RequestHeader set X-Forwarded-Proto "https"
#</VirtualHost>