How to Redirect Non-www to www URLs in .htaccess?

If you own a website, you might be wondering if using a www or non-www domain is simply a matter of user preference. However, a non-www domain has some disadvantages over a www domain. When search engines index a site, a www domain prevents split page rankings, split inbound links, and duplication of content. This improves your site's search engine optimization (SEO). The process of redirecting a non-www URL to a www URL may appear daunting, but it's not so complicated. Read on to learn three methods of redirecting If you are a beginner, it is recommended that you use the Redirect settings or edit the .htaccess file to redirect non-www to www URLs.

The Purpose of Redirecting Non-www URLs to www

There are some drawbacks to sticking with a non-www domain, including no overload protection and no control over cookies. CNAME records are not available for non-www domains, so you cannot redirect traffic from them to a healthy server if your server becomes overloaded. In this case, visitors may not be able to access your website, which may negatively affect its SEO.

Aside from that, search engines consider http://yourdomain.com and https://www.yourdomain.com to be separate websites. as a result, they require different SEO strategies. Non-www domains cannot use cookie-free subdomains unless a separate static domain is created. By contrast, a www URL allows you to restrict cookies to a specific subdomain or the root domain, reducing the number of HTTP requests and improving website performance.

3d websites

Arashtad Custom Services

In Arashtad, we have gathered a professional team of developers who are working in fields such as 3D websites, 3D games, metaverses, and other types of WebGL and 3D applications as well as blockchain development.

Arashtad Services
Drop us a message and tell us about your ideas.
Fill in the Form
Blockchain Development

 

How to Redirect Non-www to www URLs

The control panel of your hosting account, a content delivery network (CDN), or web server software can be used to redirect non-www URLs to www. Ensure that you have a backup of your website files before beginning this tutorial.

Redirect Non-www to www URLs with cPanel

If you are a beginner, it is recommended that you use the Redirect settings or edit the .htaccess file to redirect non-www URLs to www:

1. Log into your cPanel and navigate to Domains -> Redirects.

2. Select Permanent (301) under Type.

3. On https?://, provide the domain you would like to redirect. Leave the path section (/) blank.

4. Enter the URL of your website's www in the Redirects to the field.

5. Uncheck Wild Card Redirect and Choose Do Not Redirect www.

 How to Redirect Non-www to www URLs in .htaccess

6. Click the Add button.

By editing the .htaccess file, you can set the redirect manually if the above method fails:

1. Click Files -> File Manager in your cPanel dashboard.

2. Double-click the public_html folder.

3. Select Edit from the menu after right-clicking .htaccess.

4. Add the following code under <RewriteEngine On>:


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

 How to Redirect Non-www to www URLs in .htaccess

Make sure yourdomain.com is replaced with your real domain name.

5. Save Your Changes.

Redirect Non-www to www URLs with hPanel

Redirecting a non-www URL to a www URL can be done via FTP, SSH, or your hosting account's control panel. Users of hPanel can easily edit the .htaccess file by following these steps:

1. Open the File Manager from your hPanel.

2. Right-click the .htaccess file in the public_html directory and select Edit.

3. Type these codes after the RewriteEngine On:


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

 How to Redirect Non-www to www URLs in .htaccess

4. Make sure yourdomain.com is replaced with your actual domain name.

All non-www visitors will now be redirected to your www version of your website by this .htaccess rule.

Redirect Non-www to www URLs with NGINX

Here's how to redirect non-www URLs to www using virtual private server hosting and NGINX:

1. Connect to your VPS account via SSH, such as PuTTY.

2. Navigate to the directory /etc/nginx/.

3. Use the following command to view the contents of the directory:


[server]$ sudo ls - la

4. Type your NGINX password.

5. Edit nginx.conf by typing the following command:


[server]$ sudo nano nginx.conf

6. Now add the following lines of code, replacing yourdomain.com with your actual domain name:


server {
server_name yourdomain.com;
return 301 
$scheme://www.yourdomain.com$request_uri;
}

7. Restart NGINX by typing the following command:


sudo systemctl restart Nginx

Redirect Non-www to www URLs with Apache

Similar to the previous tutorial, you can edit the .htaccess file via an SSH terminal if you're using a VPS and Apache. You will need root access with sudo privileges and a text editor before proceeding. The steps will be a little different since Apache does not allow the use of .htaccess files by default:

1. Run the following command to enable mod_rewrite:


$ sudo a2enmod rewrite

Restart Apache after that:


$ sudo systemctl restart apache2

2. Use the following command to enable .htaccess:


$ sudo vi /etc/apache2/sites-available/000-default.conf

This code should be added before the <VirtualHost> line:



Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted

Then, restart Apache.

3. To create the .htaccess file, enter the command:


$ sudo vi /var/www/html/.htaccess

4. Add these lines to the file:


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

5. Restart Apache.

Those visiting the non-www URL should now be directed to the www version.

Conclusion

In most cases, people leave out the www when they type a website's address. Although it might not cause significant issues, it is best to redirect site visitors to its www version. The reason for this is that using non-www domains might negatively impact your site's performance and SEO.

In this article, you've learned to redirect non-www URLs to www via your hosting account’s control panel – hPanel and cPanel and for VPS users, via NGINX and Apache. Although it may sound complicated, the redirect process only takes a minute or two. Now, anyone accessing your site using http://yourdomain.com will be redirected to http://www.yourdomain.com.

3d websites

Arashtad Custom Services

In Arashtad, we have gathered a professional team of developers who are working in fields such as 3D websites, 3D games, metaverses, and other types of WebGL and 3D applications as well as blockchain development.

Arashtad Services
Drop us a message and tell us about your ideas.
Fill in the Form
Blockchain Development

 

0 Comments

    Leave a Reply

    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>