Search code examples
.htaccesshttp-redirecturl-rewritingno-www

Remove www. in directory only


I'm trying to remove the www. for a single directory called dir (for example). I need to be able to do this from the .htaccess file in that directory. (I don't have root access.) Any idea how to make this work?

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

Update—the solution:

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

Solution

  • The HTTP_HOST will not contain the path being accessed you need to match that in the rewrite rule itself:

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