title: Redirecting http://example.com to http:/www.example.com while keeping http://subdomain.example.com access intact
body: <p>I have a website on which I've enabled subdomain access such as:</p>
  
  <pre><code> http://subdomain1.example.com
  </code></pre>
  
  <p>which accesses the same code, but passing a domain parameter in order to show a different microsite. The httpd.conf code for that looks like this:</p>
  
  <pre><code> RewriteCond %{HTTP_HOST} ^([^./]+)\.example\.com$
   RewriteRule forums.html$ /browse.php?type=forums&amp;domain=%1 [QSA]
  </code></pre>
  
  <p>Now I need to redirect <a href="http://example.com" rel="nofollow noreferrer">http://example.com</a> to <a href="http://www.example.com" rel="nofollow noreferrer">http://www.example.com</a></p>
  
  <p>I tried this, but it did not work:</p>
  
  <pre><code>   RewriteCond %{HTTP_HOST} ^example\.com
     RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
  </code></pre>
  
  <p>(source: <a href="http://www.cyberciti.biz/faq/apache-redirect-domaincom-to-wwwdomaincom/" rel="nofollow noreferrer">http://www.cyberciti.biz/faq/apache-redirect-domaincom-to-wwwdomaincom/</a> )</p>
  
  <p>EDIT1</p>
  
  <pre><code>&lt;VirtualHost IPADDRESS:80&gt;
      ServerAlias *.example.com
      DocumentRoot /var/www/html/abc
      ServerName www.example.com
      UseCanonicalName On
  </code></pre>
  
  <p>EDIT2</p>
  
  <p>Hi mreithub,</p>
  
  <p>The setup I need is something like this:</p>
  
  <p><a href="http://X1.example.com" rel="nofollow noreferrer">http://X1.example.com</a> should use the code in /something/X1</p>
  
  <p><a href="http://X2.example.com" rel="nofollow noreferrer">http://X2.example.com</a> should use the code in /something/X2</p>
  
  <p><a href="http://example.com" rel="nofollow noreferrer">http://example.com</a> should redirect to <a href="http://www.example.com" rel="nofollow noreferrer">http://www.example.com</a></p>
  
  <p><a href="http://www.example.com/scriptA.php" rel="nofollow noreferrer">http://www.example.com/scriptA.php</a> should use the code in /var/www/html/abc/scriptA.php</p>
  
  <p><a href="http://whateverelse.example.com/scriptA.php" rel="nofollow noreferrer">http://whateverelse.example.com/scriptA.php</a> should use the code in /var/www/html/abc/scriptA.php but be passed with a 'domain=whateverelse' parameter (but the URL on screen should show always show the domain as being <a href="http://whateverelse.example.com" rel="nofollow noreferrer">http://whateverelse.example.com</a> )</p>
  
  <p>I had asked a question on SF - <a href="https://serverfault.com/questions/408805/configuring-httpd-conf-to-handle-wildcard-domains-with-multiple-scripts">https://serverfault.com/questions/408805/configuring-httpd-conf-to-handle-wildcard-domains-with-multiple-scripts</a> - from where I used adaptr's technique to pass the domain parameter to the PHP scripts.</p>