linuxapache.htaccesswebserverhttpd.conf

How can I automatically redirect HTTP to HTTPS on Apache servers?


I am trying to set up automatic redirection from HTTP to HTTPS:

From manage.mydomain.com --- To ---> https://manage.mydomain.com

I have tried adding the following to my httpd.conf file, but it didn't work:

 RewriteEngine on
    ReWriteCond %{SERVER_PORT} !^443$
    RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]

How can I fix it?

Environment: CentOS with Apache


Solution

  • I have actually followed this example and it worked for me :)

    NameVirtualHost *:80
    <VirtualHost *:80>
       ServerName mysite.example.com
       Redirect permanent / https://mysite.example.com/
    </VirtualHost>
    
    <VirtualHost _default_:443>
       ServerName mysite.example.com
      DocumentRoot /usr/local/apache2/htdocs
      SSLEngine On
     # etc...
    </VirtualHost>
    

    Then do:

    /etc/init.d/httpd restart