Redirecting non-www to www URLs is a common practice used to ensure that all traffic to your website is directed to a single domain. Here are the steps to redirect non-www to www URLs:
Determine your web server type: The steps to redirect non-www to www URLs will depend on the type of web server you are using. The two most common types are Apache and NGINX.
Create a redirect rule: Once you know your web server type, you can create a redirect rule that will redirect all non-www traffic to the www version of your domain. Here are the redirect rules for both Apache and NGINX:
Apache:
Add the following code to your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
NGINX:
Add the following code to your server block:
server {
listen 80;
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
}
Test the redirect: After creating the redirect rule, test it to make sure that it is working properly. You can do this by typing in the non-www version of your domain into a browser and verifying that it redirects to the www version.
Once you have completed these steps, all non-www traffic to your website should be automatically redirected to the www version of your domain. This can help ensure that all of your website traffic is directed to a single domain and can help with SEO and website analytics tracking.
https://crecentech.com/