To redirect non-www to www we often see this in .htaccess:RewriteEngine onRewriteCond %{HTTP_HOST}   !^www\.example\.com [NC]RewriteRule ^(.*)          http://www.example.com/%{REQUEST_URI} [R=301,NE,L]The problem is when we also have subdomains: http://wiki.example.com will be redirected to http://www.wiki.example.comA workaround is to add each subdomains to the regular expression. Not very nice.So I redirect from non-www to www with this:RewriteEngine onRewriteCond %{HTTP_HOST}   ^example\.com [NC]RewriteRule ^(.*)          http://www.example.com/%{REQUEST_URI} [R=301,L]The condition is negated, and we can then safely use subdomains.My question: how to have a generic .htaccess? i.e without giving the domain name? (less admin work, allow to have several domains pointing to the same directory, etc). I found something promising:RewriteEngine onRewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]RewriteRule ^(.*) http://www.%1/$1 [R=301,NE,L]But once again http://wiki.example.com will be redirected to http://www.wiki.example.com.I don't figure how to add the www only when no subdomain is specified. UPDATEThanks to people who gave answers, I finally used that:RewriteEngine onRewriteCond   %{HTTP_HOST}   !\..+\.RewriteRule   ^   http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Redirect non-www to www : .htaccess without giving the domain name
htaccess;mod rewrite
I personnaly use this:RewriteEngine onRewriteCond %{HTTP_HOST} !\..+\.RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]It checks if there is NOT 2 dots in the domain name. This is very similar to what w3d proposed, I just find it more readable. And as w3d stated, it is impossible to have a fully generic solution because nothing allow to guess how many dots your bare has. But still nicer to have a semi-generic solution.Beware of the fact local access could be broken (http://localhost). In some cases you should add something like that:RewriteCond %{HTTP_HOST} !=localhostOr whatever suitable to match your special needs. So in summary, it is impossible to have a truly generic method.