I have hundreds of domains so I needed an htaccess that would force www without knowing the exact domain name.

I got it to work, but then it started forcing www in front of ALL my subdomain URLs too, not just domain.com (ex: http://www.sub.domain.com). So I need something that will force www only if the user inputs domain.com.

I tried tweaking my original code and it's not working. Any suggestions?

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

I am just curious why you don't simply use a ServerAlias entry in the hpptd.conf file like most sites:

<VirtualHost *:80>
        ServerAdmin webmaster@txlinux.com
        DocumentRoot /var/www/html/txlinux.com
        ServerName txlinux.com
        ServerAlias www.txlinux.com
        ErrorLog logs/txlinux.com-error_log
        CustomLog logs/txlinux.com-access_log common
</VirtualHost>

shared hosting account - no access

Then you are going to have to request the addition of the ServerAlias entry from your provider. To use the htaccess file they have to first get to your web site and if www.domain.com is not redirecting there then they would never get to the point where they would access the file. Does that make sense?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.