Force Apache2 to redirect from HTTP to HTTPS

Want to redirect requests for http://www.yoursite.com to https://www.yoursite.com?

Simply change to your httpd.conf file (or if you are running Ubuntu or another distro that splits the httpd.conf file into multiple files, in your /etc/apache2/sites-available/{yoursite} configuration file. (If you are running a pretty Ubuntu install, the file is /etc/apache2/sites-available/default)

This technique still uses the rewrite engine (so you’ll need mod_rewrite module) but it places the configuration in the httpd.conf file (or its equivalent) and out of the .htaccess file.  There are many reasons you might want to do this, such as prevent it from being changed (many site configurations allow users to edit all .htaccess files but prevent them from editing the httpd.conf file) or to prevent it from being overwritten by certain web application packages (many application packages including WordPress and MediaWiki employ custom .htaccess files to provide more friendly URLs).

The change is simple, in your httpd.conf file, change the following part of your virtual host section:

root@itdoc /etc/apache2# ls
apache2.conf  conf.d  envvars  magic  mods-available  mods-enabled  ports.conf  sites-available  sites-enabled
root@itdoc /etc/apache2# cd sites-enabled/
root@itdoc apache2/sites-enabled# ls
phpmyadmin  wordpress
root@itdoc apache2/sites-enabled# ls -lt
total 0
lrwxrwxrwx 1 root root 29 Oct 16  2013 phpmyadmin -> ../sites-available/phpmyadmin
lrwxrwxrwx 1 root root 28 Oct 16  2013 wordpress -> ../sites-available/wordpress
root@itdoc apache2/sites-enabled# cat wordpress
NameVirtualHost *:80
NameVirtualHost *:443

<VirtualHost *:80>
UseCanonicalName Off
ServerAdmin  webmaster@localhost
DocumentRoot /var/www/wordpress
</VirtualHost>

<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/ssl/certs/cert.pem
ServerAdmin  webmaster@localhost
DocumentRoot /var/www/wordpress
</VirtualHost>

<Directory /var/www/wordpress>
Options +FollowSymLinks
AllowOverride All
order allow,deny
allow from all
</Directory>
root@itdoc apache2/sites-enabled# nano wordpress
root@itdoc apache2/sites-enabled# /etc/init.d/apache2 restart
[….] Restarting web server: apache2apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName
… waiting apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName
. ok
root@itdoc apache2/sites-enabled# cat wordpress
NameVirtualHost *:80
NameVirtualHost *:443

<VirtualHost *:80>
#    UseCanonicalName Off
#    ServerAdmin  webmaster@localhost
#    DocumentRoot /var/www/wordpress
RewriteEngine on
ReWriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
</VirtualHost>

<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/ssl/certs/cert.pem
ServerAdmin  webmaster@localhost
DocumentRoot /var/www/wordpress
</VirtualHost>

<Directory /var/www/wordpress>
Options +FollowSymLinks
AllowOverride All
order allow,deny
allow from all
</Directory>

#apache, #httpd, #https, #redirection, #rewrite