title: Apache Rewrite - Redirect Wildcard Subdomain and Handling Internal URL shortener
body: <p>I have problem in redirecting wild-card sub-domain and handing internal URL shortener.</p>
  
  <p>Let's say I have an internal URL shortener in my app</p>
  
  <pre><code>example.com/b/ABCDE
  </code></pre>
  
  <p>that will translate</p>
  
  <pre><code>example.com/book/12345678-the-book-name
  </code></pre>
  
  <p>the script referred by <code>/b/</code> (I use PHP framework that could handle URL rule) will translate short ID <code>ABCDE</code> to the book real ID <code>12345678</code> (and the title "The Book Name") then redirect it the book's permanent URL <code>example.com/book/12345678-the-book-name</code></p>
  
  <p>so then every time I spread a link about a book in websites like bulletin boards, micro-blogging sites, or physical media like posters or business cards, I use the short link (the <code>example.com/b/ABCDE</code>) instead of the permanent link (<code>example.com/book/12345678-the-book-name</code>).</p>
  
  <p>next, I need to redirect all wild-card sub-domain to the main domain (<code>www.example.com</code>) while maintaining the request URI, e.g.</p>
  
  <pre><code>http://random.example.com/book/11111111-some-book -&gt; http://www.example.com/book/11111111-some-book
  http://123456.example.com/book/22222222-another-book -&gt; http://www.example.com/book/22222222-another-book
  http://abcdefg.example.com/book/33333333-another-book-again -&gt; http://www.example.com/book/33333333-another-book-again
  </code></pre>
  
  <p>by adding the rule below after all the rules I used</p>
  
  <pre><code>&lt;VirtualHost *:80&gt;
  ServerName example.com
  ServerAlias *.example.com
  RewriteEngine on
  
  RewriteCond %{HTTP_HOST} !^www.example.com [NC]
  RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301]
  &lt;/VirtualHost&gt;
  </code></pre>
  
  <p>Consequently the url with example.com domain and without prefix like below</p>
  
  <pre><code>http://example.com/book/11111111-some-book
  </code></pre>
  
  <p>will translate to</p>
  
  <pre><code>http://www.example.com/book/11111111-some-book
  </code></pre>
  
  <p>And, another consequence is that if the internal URL shortener use plain domain without the prefix, it will take two redirection to resolve. For example, </p>
  
  <pre><code>http://example.com/b/ABCDE
  </code></pre>
  
  <p>will first be redirected to </p>
  
  <pre><code>http://www.example.com/b/ABCDE
  </code></pre>
  
  <p>which then be redirected to </p>
  
  <pre><code>http://www.example.com/book/12345678-the-book-name
  </code></pre>
  
  <p>Actually, I don't mind with the two-times redirection. But my SEO consultant said that the two-times redirection is bad for my site's SEO. (which I still don't know why)</p>
  
  <p>So I tried change the last rule to below</p>
  
  <pre><code>&lt;VirtualHost *:80&gt;
  ServerName example.com
  ServerAlias *.example.com
  RewriteEngine on
  
  RewriteCond %{HTTP_HOST} ^example.com [NC]
  RewriteCond %{REQUEST_URI} ^/b/(.*)$ 
  RewriteRule . index.php [L]
  
  RewriteCond %{HTTP_HOST} !^www.example.com [NC]
  RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301]
  &lt;/VirtualHost&gt;
  </code></pre>
  
  <p>I'm not very good at configuring Apache, but when I simulate above rule in <a href="http://htaccess.madewithlove.be/" rel="nofollow">http://htaccess.madewithlove.be/</a>, it works. But when I applied it to my server, it gave me <strong>400 Bad Request</strong> for the <code>example.com/p/ABCDE</code>.</p>
  
  <p>So, my questions are</p>
  
  <ol>
  <li>Is my SEO consultant right about his argument? Is there any explanation that can back him up or is there a counter-argument?</li>
  <li>Why the server gave <strong>400 Bad Request</strong>?</li>
  <li>How to fix the redirection? I want to maintain the short URL (<code>example.com/b/ABCDE</code> without <code>www</code> prefix) but still in one redirection.</li>
  </ol>