If you want visitors to always access your website using the www version of your domain, you can configure a permanent redirect in the .htaccess file.
For example:
https://yourdomain.com will automatically redirect to:
https://www.yourdomain.com Using a consistent domain format helps avoid duplicate content issues and ensures that all traffic is directed to a single preferred address.
Table of Contents
Before You Begin
The .htaccess file is usually located in your website’s root directory:
public_html If the file does not exist, you can create it manually.
Important: Before editing
.htaccess, create a backup copy of the file so you can easily restore the previous configuration if needed.
Redirect Non-WWW to WWW
Add the following rules to your .htaccess file:
# Redirect non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] How It Works
RewriteEngine Onenables URL rewriting.RewriteCond %{HTTP_HOST} !^www\. [NC]checks whether the requested hostname does not start withwww.RewriteRuleredirects the visitor to the same URL with thewwwprefix added.R=301creates a permanent redirect, which is recommended for SEO purposes.
Redirect Non-WWW to WWW with HTTPS
If your website uses an SSL certificate, it is recommended to redirect visitors directly to the secure HTTPS version:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L] Example:
https://yourdomain.com/contact becomes:
https://www.yourdomain.com/contact Testing the Redirect
After saving the .htaccess file:
- Open your website using the non-WWW address.
- Verify that the browser automatically redirects to the WWW version.
- Test multiple pages and subpages to ensure the redirect works correctly throughout the website.
Common Issues
If the redirect does not work as expected:
- Verify that the
.htaccessfile is located in the correct directory. - Check for conflicting redirect rules.
- Clear your browser cache before testing again.
- Ensure that URL rewriting is enabled on the server.
Summary
To force visitors to use the WWW version of your domain, add the following rule to the .htaccess file in your website’s root directory:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] If your website uses SSL, replace http:// with https:// to redirect visitors directly to the secure WWW version of the site.