How to redirect non-www to www.domain

eadz

Verified User
Joined
Jun 6, 2006
Messages
13
Hi there,
I need to redirect from a non-www domain name, e.g. example.com to www.example.com

In apache this is done with
<Virtualhost IP>
ServerName example.com
RedirectPermanent / http://www.example.com
</Virtualhost>

But in the custom HTTP config you cannot add a virtual host.
I am wondering what files I need to edit, and will the changes stick around if a user changes settings ?

Is there a global template I can edit to add this feature to all sites by default?

Thanks in advance
 
Both methods have the same effect. It takes you to the site you are supposed to be at. The only thing that changes is the address in the address bar.

To do what you want you have to use mod_rewrite and before you ask no I don't know how.
 
Both methods have the same effect. It takes you to the site you are supposed to be at. The only thing that changes is the address in the address bar.
... and the cookies, and the stats, and the HTTPS cert etc etc etc.

To do what you want you have to use mod_rewrite and before you ask no I don't know how.

No need for mod_rewrite, I just need to put the following code in the httpd.conf for the domain :

<Virtualhost IP>
ServerName example.com
RedirectPermanent / http://www.example.com
</Virtualhost>

My question is
a) how do I do this so that it doesn't get overwritten when a change is made in the control panel. ( because I can't use custom httpd configurations as it only lets me add stuff inside the <virtualhost> but I need to addstuff outside the <virtualhost> in the users custom httpd.conf )

and

b) is there a httpd.conf template that is used that I can modify so this is the default behaviour for new sites.
 
No need for mod_rewrite, I just need to put the following code in the httpd.conf for the domain

Then you already knew the answer.

is there a httpd.conf template that is used that I can modify so this is the default behaviour for new sites

Yes. Search for custom templates.
 
Just create an .htaccess file in your public_html directory with this content if it is for only a few sites

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

Obviously replacing example.com with your domain.

That is by far the easiest way accomplish what your after.

If you want new domains by default to redirect to www.etc then login to ssh and

Code:
cd /usr/local/directadmin/data/templates
mkdir custom
cp virtual_host2.conf custom/
cp virtual_host2_sub.conf custom/

If your running apache 1.3 then remove the 2 in the above lines.

Then open both files adding in-between the virtualhost section:

RedirectPermanent / http://www.|DOMAIN|

Close and save the file then

Code:
echo "action=rewrite&value=httpd" >> /usr/local/directadmin/data/task.queue
service httpd restart

Grant
 
Last edited:
Back
Top