Pre-requirements:
- Access to cPanel or Plesk dashboard with administrative rights.
- A domain with SSL enabled for both the naked domain (example_domain.com) and the "www" version (www.example_domain.com).
- Basic knowledge of Apache's .htaccess or virtual host configuration (for CLI steps).
Step-by-Step Guide for cPanel
1. Redirect WWW to Naked Domain Using the cPanel User Interface (UI)
- Log in to your cPanel dashboard.
- Under the Domains section, click on Redirects.
- In the Add Redirect section:
- Choose Permanent (301) as the redirect type.
- Select your domain from the dropdown menu (e.g., www.example_domain.com).
- In the Redirects to field, enter your naked domain (e.g., https://example_domain.com).
- Ensure the redirect applies to All Public Domains.
- Click Add to save your redirect.
- Test the redirection by visiting both www.example_domain.com and example_domain.com. Ensure that both are redirecting to the naked domain.
2. Redirect WWW to Naked Domain Using the cPanel CLI
- Log in to your server via SSH as the root user.
- Navigate to your website's document root:
cd /home/$user/public_html/
- Open (or create) the
.htaccess
file:vi .htaccess
- Add the following code to redirect the "www" version to the naked domain:
RewriteEngine On RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
- Save and close the file, then restart Apache:
sudo /usr/local/cpanel/scripts/restartsrv_apache --restart
- Test the redirection by visiting www.example_domain.com to confirm it redirects to example_domain.com.
Step-by-Step Guide for Plesk
1. Redirect WWW to Naked Domain Using the Plesk User Interface (UI)
- Log in to your Plesk control panel.
- In the left-hand sidebar, go to Domains, and select the domain you want to edit.
- Click on Hosting Settings.
- Under the WWW redirection section, set the option to redirect from www.example_domain.com to example_domain.com.
- Click OK to apply the changes.
- Test the redirection by visiting both www.example_domain.com and example_domain.com. Ensure both are redirecting to the naked domain.
2. Redirect WWW to Naked Domain Using the Plesk CLI
- Log in to your server via SSH as the root user.
- Navigate to the domain's web directory:
cd /var/www/vhosts/example_domain.com/httpdocs/
- Open (or create) the
.htaccess
file:vi .htaccess
- Add the following code to the file to redirect the "www" version to the naked domain:
RewriteEngine On RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]
- Save and close the file, then restart Apache:
sudo systemctl restart httpd
- Test the redirection by visiting www.example_domain.com and ensuring it redirects to example_domain.com.
Common Gotchas to Avoid
- Ensure your SSL certificates are properly configured for both the "www" and naked domains to avoid security warnings.
- Always use a 301 redirect for permanent redirections. This ensures search engines update their records to reflect the naked domain.
- Test thoroughly using different devices and browsers to ensure the redirect works as expected across the board.