When working with redirects in your htaccess, you are able to do specific things such as force a www or non-www version of your site. But since the .htaccess file is recursive, why not have some code that you can create once and have it work for all the sites on your account? This article demonstrates how you can force the non-www version of all sites on your account with a single piece of code.
Force non-www on all sites on your account
- Log into your cPanel dashboard.
- Click on the File Manager icon located in the Files category, selecting the Web Root as the location.
- From the web root (public_html folder) open the .htaccess file for editing. If one does not exist, create a new one, then open the blank file for editing.
- Once the file is open, paste the following code at the top of the file:
RewriteEngine on RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [R=301,QSA,NC,L]
- Save the changes to the file.
Now you can test the code by visiting the www version of your site. It should redirect in the address bar to the non-www version.
The code provided is incorrect. There is a missing newline between the “[NC]” and “RewriteRule”.
Thank you for letting us know Brett! I have updated the article to correct this.