Jump to: navigation, search

Apache Force Redirect From HTTP to HTTPS

From w3cyberlearnings

Contents

Redirect from HTTP to HTTPS

Virtual Host with HTTPS

  • Setup the HTTPS
  • Make sure the SSLengine is installed and available
<VirtualHost *:443>
     SSLEngine on
     SSLCertificateFile       /etc/apache2/ssl/my33.net.crt
     SSLCertificateKeyFile    /etc/apache2/ssl/my33.key
     SSLCertificateChainFile  /etc/apache2/ssl/gd_bundle.crt
     ServerAdmin [email protected]

     ServerName  w3cyberlearning.com
     ServerAlias do.w3cyberlearnings.com
     DocumentRoot /var/c/public
</Virtualhost>

Virtual Host without HTTPS

<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName  w3cyberlearning.com
        ServerAlias do.w3cyberlearnings.com
        DocumentRoot /var/c/public
</VirtualHost>

Force HTTP to HTTPS redirct

  • Make sure the Rewrite module is installed properly in your apache2 server
<VirtualHost *:80>
        RewriteEngine on
        RewriteCond %{SERVER_PORT} !^443$
        RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]

	ServerAdmin [email protected]
        ServerName  w3cyberlearning.com
        ServerAlias do.w3cyberlearnings.com
        DocumentRoot /var/c/public
</VirtualHost>

Restart your apache2 server

  • If you configuration is correctly, the apache2 will be able to restart without any error message.
Navigation
Web
SQL
MISC
References