View Full Version : How to redirect non-www to www.domain
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
floyd
05-28-2007, 05:18 AM
I need to redirect from a non-www domain name, e.g. example.com to www.example.com
This is the default behavior. You don't need to change anything.
This is the default behavior. You don't need to change anything.
Hi. Redirect not Alias..
I.e. when you go to http://directadmin.com/ the server shows the website.
Instead of that behaviour, what I want is the server to do a HTTP 301 redirect to www.directadmin.com ..
floyd
05-28-2007, 05:37 AM
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.
floyd
05-28-2007, 05:46 AM
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.
GranTW
05-28-2007, 07:59 AM
Just create an .htaccess file in your public_html directory with this content if it is for only a few sites
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
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
echo "action=rewrite&value=httpd" >> /usr/local/directadmin/data/task.queue
service httpd restart
Grant
Powered by vBulletin™ Version 4.0.4 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.