my subdomain.example.com
redirecting to www.subdomain.example.com
.
Htaccess code :
# ensure www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
### END WWW & HTTPS
I am looking for
example.com
towww.example.com
anything.example.com
toanything.example.com
How can i fix this?
Update : i am using one htaccess file for both subdomain and root domain and i cannot use separate htaccess file for for different domain for a reason.
Note : please do not mark this question duplicate. i have already searched other part of stackoverflow and do not find a working solution.
You need restrict your rewrite condition to match only top level domain:
# ensure www. to top level domain only
RewriteCond %{HTTP_HOST} ^[\w-]+\.(com|net|in|co\.uk)$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
### END WWW & HTTPS
Make sure to clear your browser cache before testing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With