Configuring Apache to redirect domain.com to www.domain.com

Create two files in /etc/httpd/conf.d; domain.com.conf and www.domain.conf.com.

The contents of domain.com.conf should look like this:

1
2
3
4
5
NameVirtualHost *:80
<VirtualHost *:80>
ServerName domain.com
redirect Permanent / http://www.domain.com/
</VirtualHost>

And the contents of www.domain.com.conf should look like this:

1
2
3
4
5
6
<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName www.domain.com
ServerAlias *.domain.com
ErrorDocument 403 /error/noindex.html
</VirtualHost>

Restart the webserver (service httpd restart), and your new settings will take effect.

Leave a Reply

Your email address will not be published. Required fields are marked *