Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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)

share|improve this question

migrated from stackoverflow.com Oct 31 '09 at 9:02

This question came from our site for professional and enthusiast programmers.

add comment

2 Answers

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.