.htaccess to redirect all subdomains to www

The client uses a hosting service where the httpd.conf file cannot be modified. I need the .htaccess equivalent of the following:

ServerAlias *.domain.com domain.com

Thanks

on 1clickservice.co.il hosting account I have added an alias which added a directory below root (1clickservice.com) than I’v made a new .htaccess there with the following lines:

RewriteEngine On

RewriteRule (.*) http://www.1clickservice.co.il/$1 [R=301,L]

You might try the same from subdomain folders. that is true only if you want to maintain filenames ‘uri’

Yep, that’ll work

A RewriteRule will be TOTALLY ineffective when the DNS does not point to your physical directory as will be the case with WILDCARD subdomains. If these are not supported in your control panel, then you MUST contact your webhost - it will be their decision whether or not to allow them on your account.

Regards,

DK

The only alternative if your host doesn’t support wildcard sub-domains would be to park all of the sub-domains that you want to redirect so that they can find the rewrite rule. That would be practical only if you can come up with a definitive list of all the sub-domains you want to redirect.

Thanks, but I receive an error when using eitanya’s code:

[I]The page isn’t redirecting properly

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.[/I]

True. It should be:


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

ScallioXTX: using your code, example.com redirect to www.example.com, but test.example.com doesn’t.

Then test.example.com doesn’t use the same .htaccess file and hence the RewriteRule approach won’t work.

I suggest (as dklynn suggested before) you contact your webhost with this problem to ask if they can help you out.

Thanks for the help.

My webhost has notified me that I can only use the .htaccess file to make the changes. I am hoping to find an .htaccess solution to this if possible.

So you need to park all the sub-domains on top of the domain in order that they can find the .htaccess

There is NO solution which relies solely on .htaccess (READ posts #4, #5 and #11 above).

Regards,

DK