Apache SSL Rewrite with Wildcard Subdomains
apache, mod-rewrite, ssl
Solution
Figured it out. By moving these rules from the sites-available/default file to an .htaccess inside of the website root, I was able to get this working properly.
RewriteEngine on
# Redirect domain and www to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} =mydomain.com [or]
RewriteCond %{HTTP_HOST} =www.mydomain.com
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect wildcard subdomains to HTTP
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !=www.mydomain.com
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Problem
I'm attempting to setup an SSL redirect in Apache using RewriteEngine that will do the following: - Redirect traffic to either http://mydomain.com or http://www.mydomain.com to use HTTPS - Redirect traffic to any other subdomain https://*.mydomain.com to use HTTP instead of HTTPS My reasoning for this is that I'm developing a project that's using a free SSL certificate until launch. This certificate covers the base domain, but none of the wildcard subdomains, and it's a pain to need to bypass the warning every time I visit one of the subdomains. Edit: I believe I'm close here, but I still can't get the HTTPS to HTTP redirect to work properly. ``` RewriteEngine on # Redirect domain and www to HTTPS RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} =mydomain.com [or] RewriteCond %{HTTP_HOST} =www.mydomain.com RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] # Redirect wildcard subdomains to HTTP RewriteCond %{HTTPS} on RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com$ [NC] RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] ```