Correct display of the site address!

Not all, but some search engines, such as Yandex, Google, and others, index sites differently with and without www. They perceive www.my_site.com and my_site.ua as two completely different sites, although they are actually the same site.

This article will discuss how to redirect the entered website address to the www address.

In any case, you should ask yourself why this is necessary and how it can positively impact the site. The answer is simple: it’s necessary to improve site indexing and increase site citations.

How It Works

When a user enters an address like my_site.com or my_site.com/content/view/12/11/ into the address bar, they will be automatically redirected to the same address, but with www. This happens instantly because we use Apache capabilities.

Practice

Open the .htaccess file and add the following code:

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

If you need to do the opposite, then you should write it like this:

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

In these rules:

RewriteCond %{HTTP_HOST} ^my_site\.com checks if the current host is my_site.com.

RewriteRule ^(.*)$ http://www.my_site.com/$1 [R=301,L] redirects all requests to www.my_site.ua with a permanent (301) redirect and stops further processing with L.

For the reverse redirection:

RewriteCond %{HTTP_HOST} ^www.my_site\.com checks if the current host is www.my_site.com.

RewriteRule ^(.*)$ http://my_site.com/$1 [R=301,L] redirects all requests to my_site.com without www, also with a permanent (301) redirect and stops further processing with L.

Make sure to replace my_site.ua and www.my_site.com with your actual domain name. This setup will help in ensuring that your site is properly indexed and that all traffic is directed to a single preferred version of the domain.