0

I had set up 2 domain virtualhost files under /etc/apache2/sites-available,

www.example.com.conf
example.com.conf

apart from the default configurations. I've been trying to create a redirection from example.com to www.example.com and it doesn't seem to work, I don't know where I'm going wrong.

In www.example.com.conf

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName www.example.com
    DocumentRoot /var/www/project
</VirtualHost>

In example.com.conf

<VirtualHost *:80>
    ServerName example.com
    ServerAdmin admin@mailzak.com
    DocumentRoot /var/www
    Redirect / http://www.example.com/
</VirtualHost>

After adding them, I had configured with "a2ensite" and restarted Apache. But the redirection doesn't work.

1 Answers1

0

Provided that www is a canonical alias of the naked domain, you don't really need two .conf files.

You only need one .conf file

<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/project
</VirtualHost>

Which can be activated with a2ensite example.com.

Then create an .htaccess file in /var/www/project

Options +FollowSymLinks -Multiviews
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
jonbaldie
  • 201