Toni Michel Caubet
Toni Michel Caubet

Reputation: 20173

RewriteCond not working to remove www. from subdomain URL

RewriteEngine On

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

why when i enter http://www.new.domain.com is not redirected to http://new.domain.com?

The .htaccess file is in the right folder (it has more rewritecond's and they work)

Upvotes: 1

Views: 1288

Answers (1)

Ilmari Karonen
Ilmari Karonen

Reputation: 50368

The regexp syntax in your RewriteCond is slightly broken: the correct way to test for strict equality with www.new.domain.com (up to differences in case) is either

RewriteCond %{HTTP_HOST} ^www\.new\.domain\.com$ [NC]

or

RewriteCond %{HTTP_HOST} =www.new.domain.com [NC]

That said, those errors should not stop your rewrite rule from working: you original RewriteCond will match www.new.domain.com just fine, it just matches some other strings too (like wwwXnewYdomainZcomFOOBAR). In fact, I have a very similar set of rules in my own .htaccess file, and they work just fine:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^vyznev\.net$
RewriteCond %{HTTP_HOST}  ^(www\.)?vyznev\.net$ [NC]
RewriteRule ^(.*) http://vyznev.net/$1 [NS,L,R=permanent]

Most of the differences between your code and mine are purely cosmetic. The only potentially significant issue I can see if that you don't have a RewriteBase directive; you should definitely add one, if only to avoid potential problems later. Still, as far as I can tell, not having one shouldn't stop you from getting at least some redirect, even if it might not be to the URL you expect.

Upvotes: 2

Related Questions