Having a difficult time finding the combination to satisfy the following 3 conditions. What Rewrite rules and conditions will accomplish the conditions? (I’ve already been surprised by the rules not working.)
- www stripped from all requests
- https for all requests to primary
- http for all requests to subdomain (in subfolder of main site) subdomain.com
htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.primary\.mobi [NC,OR]
RewriteCond %{HTTP_HOST} ^primary\.mobi [NC,OR]
RewriteCond %{HTTP_HOST} !^(www\.)?subdomain\.com [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The above do not strip www and send http://www.subdomain to https.
Explanations welcomed. Trying to understand the apache mod_rewrite manual page and have tried several methods without success.
You can capture the domain and use it in your RewriteRule. HTTP_REQUEST is not available in the substitution part, but only in RewriteCond directives.
I’m not sure, but you can try to split this into two
.htaccessfiles. This one goes into the main directoryand this is for the
.htaccessin the subdomain folderTest your rules without 301, because the browser caches 301 results and makes testing much harder. Add
R=301not until you’re satisfied with the rules.In Canonical Hostnames are some alternatives described, especially the first one, using virtual hosts, looks promising
I don’t know, if this is feasible for you, but you might try.