How to Redirect non-www to www URL using .htaccess in cPanel Print

  • Updated on 20-Dec-2024
  • 5132

You can redirect a non-www URL to a www URL by using rewrite rules in a .htaccess file, which manages web server settings like redirects. You can do it through the use of rewrite rules in a .htaccess file. The .htaccess file is a configuration file used to manage web server settings, such as redirects.

To redirect your site from the non-www to the ‘www’ version, add the following code to your sites’ .htaccess file.

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

This rule tells the server to turn on URL rewriting, check if the hostname does not start with “www,” and redirect the request to the equivalent URL with “www” included. The “R=301” flag indicates that the redirect should be permanent, and the “L” flag indicates that this is the last rule to be applied.

You can learn how to access and edit the .htaccess file using the cPanel by the following link below:

https://intohost.com/kbimages/how-to-access-and-edit-htaccess-file-using-cpanel/

 

The .htaccess file is a powerful tool for managing a website, and redirecting non-www URLs to www URLs is a common use case. As always, it is essential to be cautious when making changes to your website’s configuration and to thoroughly test any changes before implementing them on a live site.


Was this answer helpful?

« Back