0

Inside virtuahost apache conf file /etc/apache2/sites-available/000-default.conf.

I tried to put these code inside apache conf file.

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

Also I tried.

< If "%{HTTP_HOST} != 'YOUR-DOMAIN.com'">
Redirect "/" "http://www.YOUR-DOMAIN.com/"
</If>

Also did this.

<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / http://www.example.com/
</VirtualHost>
<VirtualHost *:80>
    ServerName www.example.com
    # real server configuration
</VirtualHost>
0

To resolve your problem you have to understand the concept of 'NameVirtualHosting' and Ubuntu's special way of deploying the apache web service.

NameVirtualHost

Your (local) apache webserver can serve different web sites on the same adress as long as they are 'named' differently. The webserver tries to match the Host name (e.g. example.com) with a corresponding Servername or ServerAlias directive in you apache configuration. For each 'website' you need a separate <VirtualHost> container in your configuration; in your case you need one for example.com and another for www.example.com.

Apache Webserver and Ubuntu

The Ubuntu apache2 server package does alter the default binyries, configuration files and directories:

  • The Apache Webserver binary is /usr/sbin/apache2
  • The server root directory is /etc/apache2
  • The main configuration is /etc/apache2/apache2.conf
  • Additional server configurations go to /etc/apache2/conf.d
  • Module Configurations are in /etc/apache2/mods-available/
  • VirtualHost sites are configured in /etc/apache2/sites-available/

So far, so easy; now comes the tricky bit (for me that is): with Ubuntu you get four commands to enable/disable modules and (!) VirtualHosts configuration:

  • a2ensite / a2dissite
  • a2enmod / a2dismod

with these commands you enable the actual websites, that you have configured in /etc/apache2/sites-available/. There is at least one default virtualhost configuration default.conf which, when enabled by a2ensite default is linked as sites-enabled/000.default.conf.

And it all works, because the main config apache2.conf contains this Include directive:

Include sites-enabled/

Let's get practical:

Remove all symbolic links in sites-enabled/ and if you happen to have stored actual config files in sites-enabled/ move those to sites-available (be careful not overwrite existing files in sites-available/ rather rename the old ones).

Now, you create two new virtualhost files in sites-available/:

vi /etc/apache2/sites-available/0_www.example.com
<VirtualHost *:80>
    ServerName www.example.com

    DocumentRoot /var/www
    DirectoryIndex index-www.html
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error-www-example-com.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access-www-example-com.log combined
</VirtualHost>

vi sites-available/1_example.com 
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName example.com

    ErrorLog ${APACHE_LOG_DIR}/error-example-com.log
    LogLevel warn
    CustomLog ${APACHE_LOG_DIR}/access-example-com.log combined

    RewriteEngine On
    RewriteRule ^/(.*) http://www.example.com/$1 [R=302]
</VirtualHost>

and enable them and the script creates a symbolic link from sites-available/ to sites-enabled/:

sudo a2ensite 0_www.example.com

sudo a2enmod rewrite

sudo a2ensite 1_example.com

(restart the apache2 service - service apache2 restart or systemctl restart apache2)

ls -l sites-enabled/
total 0
lrwxrwxrwx 1 root root 30 Jan  8 17:59 example.com -> ../sites-available/example.com
lrwxrwxrwx 1 root root 34 Jan  8 17:52 www.example.com -> ../sites-available/www.example.com

We need simple index pages

# cat /var/www/index-non.html
<html><body><h1>example.com</h1>
This is the NON-www index page
</body></html>
# cat /var/www/index-www.html
<html><body><h1>www.example.com</h1>
This is the www.example.com index page.
</body></html>

Please keep in mind, that the first <VirtualHost >directive encountererd by the apache webserver will be the default Webcontainer, so the order of the VirtualHost directives is important; in our case, if I call the webserver with no specific hostname or a hostname, that is not configured, I am being served the www-index because it is the first virthost directive.

# No Hostname given
curl http://127.0.0.1/ 
<html><body><h1>www.example.com</h1>
This is the www.example.com index page.
</body></html>

# index-www
curl -H "Host: www.example.com" http://127.0.0.1/ 
<html><body><h1>www.example.com</h1>
This is the www.example.com index page.
</body></html>

# non-www index
curl -H "Host: example.com" http://127.0.0.1/
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="http://www.example.com/">here</a>.</p>
<hr>
<address>Apache/2.2.22 (Ubuntu) Server at example.com Port 80</address>
</body></html>

(Please note the use of the Host Header variable -H)

To keep in mind is that for NameVirtualHosting to work, both webserver and webclient have to be correctly configured.

I hope my instructions can help you to solve your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.