Ssl – How to redirect two domains, with/without https, to one https site

apache-2.2mod-rewriteredirectssl

I'd like to do the following: using Apache, redirect all of the following URLs:

http://example.com
http://www.example.com
http://beta.example.com
https://example.com
https://www.example.com
https://beta.example.com

to the same place: https://beta.example.com. I'm close, but not quite there. Here's what I've got:

NameVirtualHost xxx.xxx.xxx.xxx:80
NameVirtualHost xxx.xxx.xxx.xxx:443

### rewrite scheme via http://stackoverflow.com/questions/1100343/apache-redirect-from-non-www-to-www                                    
<VirtualHost xxx.xxx.xxx.xxx:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/html/example
    CustomLog /var/log/httpd/example-log combined
    ErrorLog /var/log/httpd/example-log
</VirtualHost>

<Directory /var/www/html/example>
    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ https://beta.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    Options -Indexes
    Options +FollowSymLinks
    DirectoryIndex index.php
</Directory>

<VirtualHost xxx.xxx.xxx.xxx:80>
    ServerName beta.example.com
    DocumentRoot /var/www/html/example-beta
    CustomLog /var/log/httpd/example-beta-log combined
    ErrorLog /var/log/httpd/example-beta-log

    <IfModule mod_rewrite.c>
              RewriteEngine on
              # redirect all http -> https                                                                                             
              RewriteCond %{HTTPS} off
              RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
    </IfModule>
</VirtualHost>

<VirtualHost xxx.xxx.xxx.xxx:443>
    ServerName beta.example.com
    DocumentRoot /var/www/html/example-beta
    ErrorLog /var/log/httpd/example-beta-log
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\" %{SSL_PROTOCOL}x %{SSL_CIPHER}x" combined_ssl
    CustomLog /var/log/httpd/example-beta-log combined_ssl

    SSLEngine on
    ### SSL details omitted for clarity.
</VirtualHost>

What I get from this:

http://example.com => https://beta.example.com
http://www.example.com => Content for www.example.com (/var/www/html/example)
http://beta.example.com => https://beta.example.com
https://example.com => https://example.com (correct content but wrong url)
https://www.example.com => https://www.example.com (correct content but wrong url)
https://beta.example.com => https://beta.example.com

It's probably worth noting that my ssl certificate is fully wildcard-able, so I should be good there, and all the SSL stuff seems to be working (even when the pages are being served up under https://example.com). Meanwhile, there are no .htaccess files in place — I'm trying to do everything through apache config.

So the problem is getting https://example.com to redirect correctly, with and without the www prefix. Any advice out there? Thanks!

Best Answer

From what I'm reading, you would need two separate VirtualHost entries, one for HTTP, and one for HTTPS. These are different by the port (:80 for HTTP and :443 for HTTPS), and also because you have the SSL On section for HTTPS. Each would need to have all names listed:

ServerName  beta.example.com
ServerAlias example.com www.example.com

Then, you would have all the same DocumentRoot and other information listed there. For the HTTP VirtualHost, you already know that you need to redirect, because you know you want HTTPS, so you only need the RewriteRule:

RewriteRule ^ https://beta.example.com%{REQUEST_URI} [R=301,L]

For the HTTPS VirtualHost, you would need the same RewriteRule, but you would also need to add a condition:

RewriteCond %{HTTP_HOST} !^beta\.example\.com$
RewriteRule ^ https://beta.example.com%{REQUEST_URI} [R=301,L]

The RewriteCond is there to prevent an unending redirect.

This would allow you to control the redirects in a single place. If you don't have some reason to make a VirtualHost for each domain, then it's just simpler to have one.

Related Topic