title: 301 Redirect domain without www works, but with www does not
body: <p>Currently I'm working on a request to both redirect the domain A to domain B and redirect a specific page within the domain A to domain B.  Here's the expectation in pseudocode:</p>
  
  <pre><code>foo.com =====&gt; https://www.bar.com
  foo.com/foo =====&gt; https://www.bar.com/bar
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>www.foo.com =====&gt; https://www.bar.com
  www.foo.com/foo =====&gt; https://www.bar.com/bar
  </code></pre>
  
  <p>However, only the non-www redirects are working as expected and the www redirects are not functioning at all.  Essentially, the first set of redirects above are working and the second set are not.</p>
  
  <p>Here's my Vhost file:</p>
  
  <pre><code>&lt;VirtualHost *:80&gt;
    ServerName foo.com
      ServerAlias www.foo.com
      ErrorLog /etc/httpd/logs/foo.com-error.log
    Include /etc/httpd/conf.d/rewrite_rules/foo.com.rewrite
  &lt;/VirtualHost&gt;
  </code></pre>
  
  <p>My rewrite rule:</p>
  
  <pre><code>RewriteEngine On
  RewriteRule ^/foo$  https://www.bar.com/bar  [R=301,L,NC]
  RewriteRule (.*) https://www.bar.com  [R=301,L,NC]
  </code></pre>
  
  <p>What's strange is when I test with curl on a test server, it seems as though the URL is being redirected:</p>
  
  <pre><code>301 http://www.foo.com/ https://www.bar.com
  </code></pre>
  
  <p>I get the feeling I'm missing something in my rewrite rule.  Can anyone advise on the problem?  Thanks in advance for your help.</p>