I’m new to editing htaccess files. What I want to do is:
rewrite/redirect specific subdomains eg subdomain1, subdomain2, etc from:
https://www.subdomain.example.com
http://www.subdomain.example.com
http://subdomain.example.com
To:
https://subdomain.example.com
I’ve pieced the below code together from other questions, but it doesn’t seem to be working.
Can anyone explain what I’m doing wrong and help me fix the code.
RewriteEngine on
RewriteCond %{HTTPS} !on
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{HTTP_HOST} ^subdomain.example.com$ [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
2
Answers
You can use the following :
This will redirect all subdomains from
http://www
tohttps://
.For the
subdomain.example.com
you can use the following :To canonicalise ("redirect") only the specific subdomain …
You would need to do it something like this near the top of your
.htaccess
file:The 3rd condition ensures that it only applies to the specific subdomain.
To make this apply to several subdomains (assuming the no-www is canonical for all subdomains) then you could modify just the 3rd condition to identify the subdomains that it should apply to. For example:
Test with a 302 (temporary) redirect to avoid potential caching issues. You will need to clear your browser cache before testing.
This is along the right lines, but it fails to target HTTPS or the www sub-subdomain. Also, checking
%{HTTPS} !on
and%{SERVER_PORT} ^80$
are really the same thing.