0

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.

3
  • Does your configuration have a NameVirtualHost *:80 directive somewhere?
    – Zoredache
    Jun 1, 2010 at 19:05
  • Yes, it does have NameVirtualHost *:80. should it not?
    – pedalpete
    Jun 1, 2010 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, 2010 at 7:17

4 Answers 4

2

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>
1
1

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>
0

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>
1
  • Redirect is fine. mod_rewrite is not the hammer matching the nail this time.
    – joschi
    Jun 2, 2010 at 7:16
0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .