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 am currently trying to get people to go to http://a.abc.com (I send them to the https site after getting some X-HTTP-Headers and saving that in a session) I always want them to go to the a.abc.com site and if the http_host isn't a.abc.com I want them to redirect them there :)

I hope you guys can help?

<VirtualHost *:80>
        ServerName a.abc.com
        ServerAlias b.abc.com bcd.com efg.com
        DocumentRoot /var/www/wclp/public
        SetEnv APPLICATION_ENV production

        RewriteEngine On
        RewriteCond %{HTTP_HOST} !a.abc.com$
        RewriteRule ^(.*)$ http://a.abc.com/$1 [R=301, L]

</VirtualHost>

<VirtualHost *:443>
        ServerName a.abc.com
        ServerAlias b.abc.com bcd.com efg.com
        DocumentRoot /var/www/wclp/public
        SetEnv APPLICATION_ENV production

        SSLEngine on
        SSLCertificateFile /etc/apache2/ssl/crt.pem
        SSLCertificateKeyFile /etc/apache2/ssl/key.pem

        RewriteEngine On
        RewriteCond %{HTTP_HOST} !a.abc.com$
        RewriteRule ^(.*)$ http://a.abc.com/$1 [R=301, L]

</VirtualHost>
share|improve this question

2 Answers 2

up vote 1 down vote accepted

At first glace, it looks like this would work, but upon closer inspection I found a few bugs.

In your port 80 virtual host. you have !a.abc.com$. This statement will match true for b.abc.com, but it would return false for ba.abc.com. If you where to add an ^ to the expression then it will look for the string starting with the first character, so what you want is !^a.abc.com$. You probably also want to add the flag [NC] to the end of the RewriteCond has it will return a match without being case sensitive.

Now your port 443 virtual host's RewriteCond falls victim to the same stuff above. But there is something else I would question it's effectiveness. Which might be causing the whole thing to break. You have it listening for HTTPS (*.443) request and then redirecting back to HTTP. And thus you have you loop, b/c you are forwarding it in you application to HTTPS, which again, will forward it back to HTTP. Here is a quick stab of how I would rewrite (thought not 100% sure since I don't know the entire environment)

<VirtualHost *:80>
    ServerName a.abc.com
    ServerAlias b.abc.com bcd.com efg.com
    DocumentRoot /var/www/wclp/public
    SetEnv APPLICATION_ENV production

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^a.abc.com$ [NC]
    RewriteRule ^(.*)$ http://a.abc.com/$1 [R=301, L]
</VirtualHost>

<VirtualHost *:443>
    ServerName a.abc.com
    ServerAlias b.abc.com bcd.com efg.com
    DocumentRoot /var/www/wclp/public
    SetEnv APPLICATION_ENV production

    SSLEngine on
    SSLCertificateFile /etc/apache2/ssl/crt.pem
    SSLCertificateKeyFile /etc/apache2/ssl/key.pem

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^a.abc.com$ [NC]
    RewriteRule ^(.*)$ https://a.abc.com/$1 [R=301, L]
</VirtualHost>

No unless you are doing something crazy, like not use SSL on port 443, or using it on port 80. Then this will work. If you are accepting non-ssl transmissions over port 443 then 1) your crazy :) and 2) you can check to see if it is a secure channel using the %{HTTPS}

Here are two great links, from Apache:

I use the Reference Documentation a lot when writing complex rules, and use the guide when it's been a while and need a quick refresher.

Hopefully this helps.

share|improve this answer

this should work, but only if there are only those two virtual hosts configured. If you have other name based virtual hosts, the first vhost will always kick in if HTTP_HOST is not matched.

share|improve this answer
    
There are bugs in it, but your comment about the vhost is insightful for those not use to apache configs –  Ryan Gibbons Jan 16 '10 at 16:36

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.