title: Apache: Redirect WWW to naked domain
body: <p>I have an application that was written using Laravel, and it generates dynamic subdomains. Thus, I have a wildcard(<code>*</code>) routing mechanism. Due to several complications I'm having with SSL, I need to redirect the <code>www.example.com</code> routes to the naked domain (<code>example.com</code>), preferrably in the Apache .conf files using RewriteRules.</p>
  
  <p>I've done a few searches and seen a few examples, but most of what I've seen either redirects <em>all</em> subdomains to the naked domain (which breaks the subdomain functionality I stated above) or does the reverse of what I'm trying to do (redirects the naked domain to www) which causes a redirect loop in many of my existing routes.</p>
  
  <p>Is this something that can be done using Apache?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I think I fixed it. For anyone else having similar problems, I used the following rules in <code>/etc/apache2/sites-enabled/000-default.conf</code>:</p>
  
  <pre><code>    RewriteEngine On
      RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
      RewriteRule ^(.*)$ http://%1$1 [R=301,L]
  </code></pre>
  
  <p>This redirects any URL that starts with <code>www</code> to its naked equivalent, which has the added benefit of stripping <code>www</code> from subdomains (so, for example, <code>www.test.example.com</code> is redirected to <code>test.example.com</code> which is great (although unnecessary) since <code>www.{subdomain}.example.com</code> is not a defined route in my app and won't work anyways.</p>