how to add canonical url to website
eg: site.com to be converted into www.site.com
this can be done by ServerAlias in virtual host or any other way to set canonical url to website.

Knowledge Base
Home » Knowledge Base » Developer Corner » Apache web server » Here
How to add or remove the www prefix in domain URLs

This article describes how to add or remove the www prefix in your domain's URLs. For example, if users type www.example.com in their browsers, you can automatically redirect them to example.com, or vice versa.

For Search Engine Optimization (SEO) purposes, it is preferable to have a website respond only to http://www.example.com or http://example.com, not both. Otherwise, search engines see duplicate content on the www and non-www domains, and may downgrade the site ranking.
Table of Contents

Adding the www prefix to domain URLs
Removing the www prefix from domain URLs
Related Articles

Adding the www prefix to domain URLs

You can automatically add the www prefix to your domain's URLs by using Apache rewrite rules in a custom .htaccess file. To do this, follow these steps:

Create an .htaccess file in your public_html directory. If you already have an .htaccess file in the public_html directory, you can modify it.
For more information about how to use .htaccess files, please see this article.
Copy and paste the following lines of text into the .htaccess file:

# Add www to any URLs that do not have them:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Save the .htaccess file.
To test the configuration, use your web browser to visit http://example.com, where example.com represents your domain name. The browser should be redirected to http://www.example.com.

using ServerAlias in apache or virtual host
i.e namebased virtual host
how to have a website http://site.com to http://www.site.com
this can be done through ServerAlias .can any one say with example?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.