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!
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:
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:
For the HTTPS VirtualHost, you would need the same RewriteRule, but you would also need to add a condition:
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.