How do you redirect only the HTTP subdomain to HTTPS subdomain in .htaccess
? The main site is in WordPress and already redirected to HTTPS with a plugin, but the subdomain is a PHP created site. I have looked and not seen a conclusive solution to this on here.
I copy and pasted this suggested code but it did nothing:
#Redirect Subdomain
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain.example.com$ [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
2
Answers
Please try one of the followings:
OR
you dont need to edit following
This would create a redirect loop (if executed at all). In order to redirect from HTTP to HTTPS you need to first check you are not already on HTTPS (otherwise you will get a redirect loop).
These directives would need to go in the
.htaccess
in the document root of your subdomain. Or at the top of your root (WordPress).htaccess
file if the subdomain points to the document root of the main site. The important thing is that the redirect must go before the WordPress front-controller.This also assumes your SSL cert is installed directly on your application server and not a proxy.
Try the following:
This checks that
HTTPS
does not contain “on” and the subdomain is being requested before redirecting to HTTPS on the same host.Although if WP is simply redirecting the main domain, then you could do all this in
.htaccess
and simply remove the condition that checks againstHTTP_HOST
. Although if you have other subdomains that should not be redirected to HTTPS then alter the CondPattern to match just the subdomain or main domain (and www subdomain). For example: