Monday, January 24, 2011

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

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.

  • 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>
    
    From djdicbob
  • Use mod_rewrite:

    Your conf should look like this:

     <VirtualHost *:80> 
     DocumentRoot /var/www/html/secure
     ServerName secure.com
    
     RewriteEngine On  
     RewriteCond %{HTTPS} off  
     RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}`
    
    </VirtualHost>
    
    joschi : `Redirect` is fine. mod_rewrite is not the hammer matching the nail this time.
    From w00t
  • Try the following configuration:

    NameVirtualHost *:80
    NameVirtualHost *:443
    
    # default virtual host
    <VirtualHost *:80>
        ServerName _default_
        DocumentRoot /var/www/html/default
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerName secure.com
        DocumentRoot /var/www/html/secure
        RedirectPermanent / https://secure.com
    </VirtualHost>
    
    <VirtualHost *:443>
        ServerName secure.com
        DocumentRoot /var/www/html/secure
        SSLEngine on
        # other SSL related directives
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerName notsecure.com
        DocumentRoot /var/www/html/notsecure
    </VirtualHost>
    

    You should also take a look at the VirtualHost Examples in the Apache httpd documentation.

    From joschi

0 comments:

Post a Comment