title: Redirect all subdomains except www to main domain
body: <p>I want to redirect all subdomains except www to <a href="https://www.example.com" rel="nofollow noreferrer">https://www.example.com</a> in .htaccess, how can I do this? All subdomains can be accessed.</p>
  
  <p>I only want to redirect the subdomains, not inner pages on the main domain.</p>
  
  <p>Examples: (Should redirect)</p>
  
  <ul>
  <li>hello.domain.com</li>
  <li>test.example.domain.com</li>
  <li>www.hello.sub.domain.com</li>
  </ul>
  
  <p>Examples of where not to redirect:</p>
  
  <ul>
  <li>www.domain.com/hello-world</li>
  <li>www.domain.com</li>
  </ul>
  
  <p>Current .htaccess file:</p>
  
  <pre><code>RewriteEngine On
  RewriteCond %{HTTP_HOST} !^www.domain.dk$
  RewriteRule ^(.*) https://www.domain.dk/$1 [QSA,L,R=301]
  
  # BEGIN WordPress
  &lt;IfModule mod_rewrite.c&gt;
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.php$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.php [L]
  &lt;/IfModule&gt;
  
  # END WordPress
  </code></pre>