title: All http traffic to domain.com and all https to sub.domain.com with htaccess
body: <p>I have two domain names www.domain.com and sub.domain.com, and I want to redirect all the http traffic to www.domain.com and all the https traffic to sub.domain.com. For example, if the url called is http:// sub.domain.com/index.php, the server have to rewrite the url to http:// www.domain.com/index.php, and if the url called is https:// www.domain.com/index.php, the server have to rewrite the url to https:// sub.domain.com.</p>
  
  <p>I have this code in htaccess but it does not work correctly:</p>
  
  <pre><code>RewriteEngine On
  
  RewriteCond %{HTTPS} on
  RewriteCond %{HTTP_HOST} ^www.domain.com$ [OR]
  RewriteCond %{HTTP_HOST} ^domain.com$
  RewriteRule ^/?$ "http\:\/\/sub\.domain\.com\/" [R=301,L]
  
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP_HOST} ^sub.domain.com$
  RewriteRule ^/?$ "http\:\/\/domain\.com\/" [R=301,L]
  </code></pre>
  
  <p>It redirects correctly the https to sub.domain.com but only when there is no page indicated (https:// domain.com/index.php/some-page not work and https:// domain.com yes). For the http not work at all.</p>
  
  <p>Thanks in advance!</p>