How do you redirect non-www to www prefixed domains??

DReade83

Well-Known Member
Oct 20, 2006
196
0
166
Cheshire, UK
I've googled this and found a few examples, all of which are the same and work for the people that are trying them (but they don't have cPanel). I've tried it and it doesn't work!

RewriteCond %{HTTP_HOST} ^mydomain\.co\.uk$ [NC]
RewriteRule .* http://www.mydomain.co.uk/ [L,R=301]

Adding this to the httpd.conf file either inside the specific VirtualHost directive or at the bottom of the file, or even inside the site's htaccess file; it doesn't work!

Can anyone help?

Surely this should be a feature of cPanel too? Although I've tried to do this using cPanel's redirect function, but I get an error (see http://bugzilla.cpanel.net/show_bug.cgi?id=6250).

Thanks in advance!
 

hooter814

Member
Jun 22, 2006
8
0
151
Sorry I don't have the solution for you as I am having problems as well. Here is what I used for years on a site in an htaccess file which worked perfectly:

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

Now since moving to a WHM/CPANEL box, those lines will throw a 500 error - all other rewrite rule directives work but as soon as I add those lines, I get a 500 error. This is very standard stuff and as I said worked for years on a domain right up to moving to the cpanel box.

Thanks in advance for any guidance - perhaps I must use a different server/host environment variable?
 

hooter814

Member
Jun 22, 2006
8
0
151
I've googled this and found a few examples, all of which are the same and work for the people that are trying them (but they don't have cPanel). I've tried it and it doesn't work!

RewriteCond %{HTTP_HOST} ^mydomain\.co\.uk$ [NC]
RewriteRule .* http://www.mydomain.co.uk/ [L,R=301]
@DReade and anyone else following along - through trial and error I discovered solution which works for me - it seems cpanel server config does not like the "escaped" for reserved characters in rewrite directive. Here is what I had before which worked on previous servers without issue:

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

Here is what I changed it to for it to work without throwing 500 Internal Server error on cpanel box:

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