Apache VirtualHost, multiple sites. 1 ssl with redirect and 1 regular http

13,931

Solution 1

If you have NameVirtualHost on, you have to use the IP. NameVirtualHost is needed if you are running SSL, or running VirtualHosts on different IP addresses.

<VirtualHost 172.16.4.1:80>
     DocumentRoot /var/www/html/secure
     ServerName secure.com
     Redirect / https://secure.com
</VirtualHost>

<VirtualHost 17.16.4.1:80>
    DocumentRoot /var/www/html/notsecure
    ServerName notsecure.com
</VirtualHost>

Solution 2

Well, you need to declare this (best in httpd.conf/apache2.conf), this is important! DO NO USE ASTERISK "*"

NameVirtualHost IP_or_hostname:80
NameVirtualHost IP_or_hostname:443

And in your virtual host:

<VirtualHost IP_or_hostname:443>
        ServerName whateveryouwant
        DocumentRoot /www/blahblah/

        SSLEngine on
        SSLCertificateKeyFile /etc/apache2/ssl/blahblah.key
        SSLCertificateFile /etc/apache2/ssl/blahblah.crt
        SSLProtocol all
        SSLCipherSuite HIGH:MEDIUM

        ErrorLog /var/log/apache2/error.log

</VirtualHost>

<VirtualHost IP_or_hostname:80>
        ServerName whateveryouwant
        DocumentRoot /www/blahblah

        ErrorLog /var/log/apache2/error.log

        LogLevel warn
        ServerSignature Off

        RewriteEngine on
        RewriteCond %{HTTP_HOST} ^blahblahblah$ [NC]
        RewriteRule ^/(.*) https://blahblahblah/$1 [R=301,L]

</VirtualHost>
Share:
13,931

Related videos on Youtube

pedalpete
Author by

pedalpete

Originally from Whistler, Canada, now living in Bondi Beach, Aus. I like building interesting things, algorithms, UX/UI, getting into hardware and RaspberryPi.

Updated on September 17, 2022

Comments

  • pedalpete
    pedalpete over 1 year

    I've got a server with one site which I am redirecting to https via

    <VirtualHost *:80>
         DocumentRoot /var/www/html/secure
         ServerName secure.com
         Redirect / https://secure.com
    </VirtualHost>
    

    That works no problem.

    Now I'm trying to add another non-secure site

    <VirtualHost *:80>
        DocumentRoot /var/www/html/notsecure
        ServerName notsecure.com
    </VirtualHost>
    

    of course, because the redirect is on '/', all sites are getting redicted. I've tried changing the Redirect to the full document root, but no luck.

    • Zoredache
      Zoredache almost 14 years
      Does your configuration have a NameVirtualHost *:80 directive somewhere?
    • pedalpete
      pedalpete almost 14 years
      Yes, it does have NameVirtualHost *:80. should it not?
    • joschi
      joschi almost 14 years
      No, that's fine. The NameVirtualHost directive needs to be there in order to use name-based virtual hosting.
  • joschi
    joschi almost 14 years
    Redirect is fine. mod_rewrite is not the hammer matching the nail this time.
  • Suresh Kamrushi
    Suresh Kamrushi almost 4 years
    perfect solution.