RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [R=301,L]
You've not stated exactly what is happening or where the subdomains are pointing to, so there could be other issues here.
However, if your subdomains point to the same area of the filesystem (or perhaps a subdirectory) as the main domain then the above rule will redirect all subdomains to www.example.com
- so the subdomain is "lost".
Depending on your requirements, you could resolve this by only checking for example.com
instead of !^www\.
and splitting the HTTP to HTTPS redirect into its own rule. For example:
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^(example\.com)\.?$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]
You will need to clear your browser cache before testing. (Preferably test with 302 - temporary - redirects to avoid potential caching issues.)
However, the directives are also in the wrong order (this has nothing to do with the subdomain issue). The canonical redirects (above) need to go before the internal rewrites. So, your .htaccess
file should be ordered like this instead:
IndexIgnore *
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^(example\.com)\.?$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]
RewriteCond %{THE_REQUEST} \s/(.+)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# Internally rewrite request to append ".php" extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]