1

I'm not sure if my syntax is correct here. I'm specifically looking to do this:

  1. Redirect all traffic to the root domain (mydomain.com) to a www version (www.mydomain.com).

  2. Redirect all http traffic to https on specific subdomains (or root domain to https:/www)

So if someone goes to the root domain (any page) they will go to the https://www version of that page.

If they go to two specific subdomains (www and clients) using http, they'll be redirected to the https version (either https://www. or https://clients. respectively).

If they go to any subdomain other than www or clients, then do not force https.

Here's what I came up with:

# FORCE ROOT TO WWW
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
# END FORCE ROOT TO WWW

# FORCE SSL
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www|clients)\. [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# END FORCE SSL

This doesn't seem optimal. Is there a better way?

Also, this is a WordPress site and there are other rules in htaccess. Should this go at the very top of the htaccess file.

Thanks much.

1
  • There may be a slightly better way to do this, but this looks good enough I would not worry about it. I think no matter what, you are going to end-up with something similar anyway so I would not change this code if it is working for you. It looks safe to me and I prefer safe over efficient.
    – closetnoc
    Commented Mar 2, 2015 at 23:13

0

Browse other questions tagged or ask your own question.