1

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:

<IfModule mod_rewrite.c>
    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]
</IfModule>

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.

2

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]
|improve this answer|||||
  • 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 Oct 2 '15 at 17:46
  • 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 Oct 2 '15 at 17:48
  • 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 Oct 2 '15 at 20:07
  • @subtlegusto Glad you got it sorted! Just added that hostname-agnostic version to my answer; like that one a lot better :) – Brandon Kelly Oct 5 '15 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.