<blockquote>
  <p><a href="https://craftcms.stackexchange.com/questions/6509/htaccess-www-to-non-www-redirect-and-config-php">This question</a> comes close but implementing with a subdomain doesn't work.</p>
</blockquote>

<p>It should work as long as you modify the rewrite rule to account for the subdomain:</p>

<pre><code>RewriteCond %{HTTP_HOST} !^foo\.bar\.com$ [NC]
RewriteRule ^(.*)$ http://foo.bar.com/$1 [R=301,L]
</code></pre>

<p>The code you posted was missing the <code>^</code> before <code>foo\.bar\.com</code> on the <code>RewriteCond</code> line. The purpose of that conditional is supposed to be to catch incoming requests where the domain isn’t <em>exactly</em> “foo.bar.com” (so that it can redirect them to the correct domain on the following line). However without that <code>^</code>, the conditional would only catch requests where the domain doesn’t <em>end with</em> “foo.bar.com”.  But “www.foo.bar.com” does in fact <em>end with</em> “foo.bar.com” so it’s not going to get caught.</p>

<p>Hope that makes sense.</p>

<p><strong>Update:</strong></p>

<p>Here’s a hostname-agnostic version that’s a little easier on the eyes:</p>

<pre><code>RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</code></pre>

CC BY-SA 3.0
2015-10-02T17:46:33.370Z
Thanks @brandon-kelly. Unfortunately I'm still stuck. But I'm starting to think that this isn't related to .htaccess or Craft and that it's either a DNS or Rackspace-specific problem. I have a ticket open and will post a follow-up later.
plaintxt
CC BY-SA 3.0
2015-10-02T17:48:41.283Z
Yeah this is assuming that bar.com’s DNS record is actually set up to point www.foo.bar.com to your server, and your server is configured to route www.foo.bar.com requests to the same host as foo.bar.com.
Brandon Kelly
CC BY-SA 3.0
2015-10-02T20:07:18.707Z
WHEW... so this turned out to mainly be a Rackspace routing issue. The support guy said that an entry for `www.foo.bar.com` needed to be added to the load balancer. Without that, the request wasn't being received by our files. However, @brandon-kelly 's code does work. Rackspace also provided an agnostic version:

    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

So glad this is fixed.
plaintxt
CC BY-SA 3.0
2015-10-05T18:40:09.180Z
@subtlegusto Glad you got it sorted! Just added that hostname-agnostic version to my answer; like that one a lot better :)
Brandon Kelly
CC BY-SA 3.0
2015-10-02T17:14:36.097Z
2015-10-05T18:39:43.837Z
2017-04-13T12:51:00.253Z
Brandon Kelly
<p>Our client has (sadly) presented a new site URL as www.foo.bar.com instead of foo.bar.com. I need to use .htaccess to remove the www. The site works correctly if <code>www</code> is not prepended to the URL.</p>

<p><a href="https://craftcms.stackexchange.com/questions/6509/htaccess-www-to-non-www-redirect-and-config-php">This question</a> comes close but implementing with a subdomain doesn't work.</p>

<p>Here's my .htaccess:</p>

<pre><code>&lt;IfModule mod_rewrite.c&gt;
    RewriteEngine On
    RewriteBase /

    # Redirect to non-www domain
    RewriteCond %{HTTP_HOST} !foo\.bar\.com$ [NC]
    RewriteRule ^(.*)$ http://foo.bar.com/$1 [R=301,L]

    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (.+) index.php?p=$1 [QSA,L]
&lt;/IfModule&gt;
</code></pre>

<p>To clarify, I would like to remove both <code>www</code> and <code>index.php</code> from all URLs. So:</p>

<p><code>http://www.foo.bar.com/yay</code> -> <code>http://foo.bar.com/yay</code></p>

<p>FWIW, this is hosted on Rackspace, which requires the <code>Rewrite Base /</code> directive.</p>

CC BY-SA 4.0
2021-03-14T06:55:01.150Z
I'm having similar issue. The main domain gets redirected from www to https:// however the subdomain is not being redirected. Can you help please? Thanks
Sami
<htaccess><subdomain>
htaccess www to non-www with subdomain
# htaccess www to non-www with subdomain

(Asked by: username_0 on 2015-10-02)

Our client has (sadly) presented a new site URL as www.foo.bar.com instead of foo.bar.com. I need to use .htaccess to remove the www. The site works correctly if www is not prepended to the URL.
This question comes close but implementing with a subdomain doesn't work.
Here's my .htaccess:

    RewriteEngine On
    RewriteBase /

    # Redirect to non-www domain
    RewriteCond %{HTTP_HOST} !foo\.bar\.com$ [NC]
    RewriteRule ^(.*)$ http://foo.bar.com/$1 [R=301,L]

    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (.+) index.php?p=$1 [QSA,L]


To clarify, I would like to remove both www and index.php from all URLs. So:
http://www.foo.bar.com/yay -> http://foo.bar.com/yay
FWIW, this is hosted on Rackspace, which requires the Rewrite Base / directive.

**Tags:** htaccess, subdomain
> **Comments:**
> **username_1**: I'm having similar issue. The main domain gets redirected from www to https:// however the subdomain is not being redirected. Can you help please? Thanks  

---

## 1 answers

### Answer by username_2 on 2015-10-02. Score: 4 | Accepted answer

This question comes close but implementing with a subdomain doesn't work.

It should work as long as you modify the rewrite rule to account for the subdomain:
RewriteCond %{HTTP_HOST} !^foo\.bar\.com$ [NC]
RewriteRule ^(.*)$ http://foo.bar.com/$1 [R=301,L]

The code you posted was missing the ^ before foo\.bar\.com on the RewriteCond line. The purpose of that conditional is supposed to be to catch incoming requests where the domain isn’t exactly “foo.bar.com” (so that it can redirect them to the correct domain on the following line). However without that ^, the conditional would only catch requests where the domain doesn’t end with “foo.bar.com”.  But “www.foo.bar.com” does in fact end with “foo.bar.com” so it’s not going to get caught.
Hope that makes sense.
Update:
Here’s a hostname-agnostic version that’s a little easier on the eyes:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

> **Comments:**
> **username_0**: Thanks @brandon-kelly. Unfortunately I'm still stuck. But I'm starting to think that this isn't related to .htaccess or Craft and that it's either a DNS or Rackspace-specific problem. I have a ticket open and will post a follow-up later.  
> **username_2**: Yeah this is assuming that bar.com’s DNS record is actually set up to point www.foo.bar.com to your server, and your server is configured to route www.foo.bar.com requests to the same host as foo.bar.com.  
> **username_0**: WHEW... so this turned out to mainly be a Rackspace routing issue. The support guy said that an entry for `www.foo.bar.com` needed to be added to the load balancer. Without that, the request wasn't being received by our files. However, @brandon-kelly 's code does work. Rackspace also provided an agnostic version:      RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]     RewriteRule ^(.*)$ http://%1/$1 [R=301,L]  So glad this is fixed.  
> **username_2**: @subtlegusto Glad you got it sorted! Just added that hostname-agnostic version to my answer; like that one a lot better :)  

---

