1) A request to http://www.example.net
will have two redirects.
This can be resolved by simply reversing the two rules. Then www.example.net
is redirected to HTTPS in the first redirect, so the HTTP to HTTPS redirect does not need to trigger.
(This does, however, assume you have no intention of implementing HSTS - in which case you would need to keep them as two redirects since redirecting to HTTPS on the same hostname first is a requirement.)
2) Like most www to non-www redirect examples on this site, it won't redirect ww.
or wwww.
so my analytics has lots of mistyped subdomains that haven't been redirected.
Ordinarily, requests to ww.
or wwww.
subdomains simply won't resolve, so this is not normally an issue. For this to work you have to have configured a wildcard subdomain in DNS and configured the server to accept such requests.
But this can be accounted for by modifying the regex (snippet) from ^www\.
to ^w{2,4}\.
.
3) I'd like to exclude the dev.
subdomain from redirection, so http://dev.example.net
and its https
sibling, as I use dev.
for development and release staging.
This only applies to the HTTP to HTTPS rule, so an additional condition can be applied here to exclude hostnames that start dev.
.
Bringing the above points together, try the following:
# Remove leading ww, www or wwww (and redirect to HTTPS)
RewriteCond %{HTTP_HOST} ^w{2,4}\.example\.net [NC]
RewriteRule (.*) https://example.net/$1 [R=301,L]
# Move http to https (except dev subdomain)
RewriteCond %{HTTP:Host} !^dev\. [NC]
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP:Host}%{REQUEST_URI} [R=301,L]
I've kept your use of HTTP:Host
the same (in order to access the Host
HTTP request header) in case this is a requirement of the load balancer? Otherwise, it's more common to use the HTTP_HOST
server variable here.
The !
prefix on the CondPattern (ie. !^dev\.
) negates the regex, so the condition is successful when the Host
does not start with dev.
. (I assume www.dev.
isn't a thing?)
(.*)
is the same as ^(.*)$
since the regex is greedy by default.
You will need to clear your browser cache before testing. It is advisable to first test with 302 (temporary) redirects in order to avoid any caching issues.