1

On my old host, when adding a domain i could choose if i wanted http://www.domain, http://domain, or both (if for example i chose www.domain, it would redirect http://domain to http://www.com.)

is this possible without editing .htaccess? like in vosts.conf?

also, if doing it via .htaccess, what is the best method?

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

? i've read that without the first line, http/1.0 browsers will fail (as they don't include the host in the header)

0

2 Answers 2

2

If you're not doing anything with SSL, you should be able to use name-based virtual hosts to issue the redirects as well. One virtual host would be the desired one, and the other would be your redirect.

<VirtualHost XXX.XXX.XXX.XXX>
ServerName old.com
ServerAlias www.old.com
Redirect permanent / http://www.new.com/
</VirtualHost>


<VirtualHost XXX.XXX.XXX.XXX>
ServerName www.new.com
<Directory /var/www/www.new.com>

</Directory>
</VirtualHost>

See #6 on the following site: http://www.yolinux.com/TUTORIALS/ApacheRedirect.html

1

Anything you can put in .htaccess you can also put within a <Directory/> stanza in the VirtualHost config for the site you're trying to alter - .htaccess is just a shortcut to doing this.

For example, you can extend a Virtualhost directive like this:

<VirtualHost x.x.x.x:80>
    ServerName something.com
    ErrorLog logs/something.com-error.log
    ###### Add the 5 lines below....  ######
    <Directory /var/www/something.com/html>
        RewriteCond %{HTTP_HOST} .
        RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
        RewriteRule (.*) http://www.example.com/$1 [R=301,L]
    </Directory>
</VirtualHost>

As you mentioned "vhost.conf", I assume you're using a Plesk server, in which case you can just add the <Directory/> stanza to vhost.conf (remembering to fix the path to your site's httpdocs directory.

Remember to run /usr/local/psa/admin/bin/websrvmng -a afterwards, and restart Apache.

You must log in to answer this question.