Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
    
Does your configuration have a NameVirtualHost *:80 directive somewhere? –  Zoredache Jun 1 '10 at 19:05
    
Yes, it does have NameVirtualHost *:80. should it not? –  pedalpete Jun 1 '10 at 19:21
    
No, that's fine. The NameVirtualHost directive needs to be there in order to use name-based virtual hosting. –  joschi Jun 2 '10 at 7:17
add comment

4 Answers

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>
share|improve this answer
add comment

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.

share|improve this answer
add comment

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|improve this answer
add comment

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>
share|improve this answer
    
Redirect is fine. mod_rewrite is not the hammer matching the nail this time. –  joschi Jun 2 '10 at 7:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.