I'm fixing my Apache (2.4.12) config files on a server that serves three different domain names. I have a different config file for each site. I cannot for the life of me figure out how to accomplish both of the following:
- Redirect all
http
requests tohttps
, keeping the entire rest of the request (subdomain/host AND document path) exactly the same - Redirect all
www
requests to non-www
I've read that this can be done in one step if I have only one *:80
VirtualHost and put the rewrite rules there (the remainder of my subdomains are all *:443
VirtualHosts with the exception of www
), but I can't figure out how to do it. These answers on SO did not work:
- The accepted answer in this question is not correct (only does the
https
redirect) - This answer does not work for me--only the
https
redirect works. - This question doesn't deal with a wildcard subdomain and is thus inapplicable.
- This question is also inapplicable because it doesn't deal with subdomains.
EDIT: This is the code I reference in the comments for mike.k's answer below.
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTP_HOST} www.example.com
RewriteRule ^(.*)$ https://example.com/$1 [R=permanent,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://example.com%{REQUEST_URI}
</VirtualHost>