Reputation: 3680
I want to redirect to https://
on my website and remove any www.
subdomain in the url using htaccess on my WordPress site, because my security certificate doesn't cover the www
subdomain. This is almost working:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
RewriteRule ^ https://%3%{REQUEST_URI} [R=301,L]
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
This redirects to https://
and removes www.
from http://
urls. However, it doesn't remove the subdomain if the https://
is already there, so e.g. http://www.example.com
is changed to https://example.com
, but https://www.example.com
remains unchanged.
What am I doing wrong?
Upvotes: 0
Views: 957
Reputation: 4302
You could exclude main domain to match all sub domain + remove wwww + force https :
RewriteCond %{HTTP_HOST} !^(www\.)?maindomain\.com$
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)$
RewriteRule ^ https://%2%{REQUEST_URI} [L,R=301]
Then , If you want to force maindomain only use this :
RewriteCond %{HTTP_HOST} ^(www\.)?maindomain\.com$
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But maindomain will be forced into https as it is , if you want to remove or add www let me know
NOTE: Clear browser cache then test
Upvotes: 0
Reputation: 10859
It should be as simple as this:
RewriteEngine On
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^example.com
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L,QSA]
Upvotes: 3