I've been sitting with this a long time now and can't get my head around it.
Essentially, the functionality I'm looking for in the end would be similar to the following:
http://example.com redirects to https://example.com - currently working
and for
http://*.example.com and https://*.example.com to redirect to https://example.com/* - Not working. Currently, all subdomains get redirected to the main-domain, without their respective folder path. Also, when going to https://test.example.com, it doesn't redirect at all.
Here's the example.com.conf from sites-enabled:
<IfModule mod_ssl.c>
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
# Redirect permanent / https://example.com/
<Directory /var/www/example.com/public_html>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<IfModule mod_rewrite.c>
# Redirect all traffic to HTTPS
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,R=301,L]
</IfModule>
</VirtualHost>
<VirtualHost _default_:443>
ServerAdmin webmaster@localhost
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
<Directory /var/www/example.com/public_html>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<IfModule mod_rewrite.c>
# Remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [QSA,R=301,L]
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/%1/$1 [R=301,L]
</IfModule>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /etc/ssl/civrpssl/example.com.crt
SSLCertificateKeyFile /etc/ssl/civrpssl/example.com.key
SSLCACertificateFile /etc/ssl/civrpssl/ca-example.com.crt
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
</VirtualHost>
</IfModule>
And finally, here's the .htaccess from example.com/public_html/:
removed, see edit #2.
I'm hoping it's a rather obvious issue that I've failed to see. Otherwise, please could someone enlighten me where I should start looking for the issue? I'm assuming that the HTTPS redirect interferes with the subdomain redirect in the .htaccess file but don't know how I could put it any differently.
EDIT #1: I just noticed that www.example.com throws error "URL not found" rather than re-directing to https://example.com. How comes it isn't caught by the first rewrite in .htaccess?
EDIT #2:
- Moved rewriting from
.htaccesstoexample.com.conf(code updated in post). - Added VHost for port 80, essentially just redirecting all traffic to HTTPS. I found that Redirect worked, however it wouldn't include and subdomains in the redirect, so
http://test.example.comwould only lead tohttps://example.com. - Ran
a2enmod rewrite, since apparently it wasn't enabled.