If you want visitors to always access your website without the www prefix, you can configure a permanent redirect in the .htaccess file.
For example:
https://www.yourdomain.com will automatically redirect to:
https://yourdomain.com This helps maintain a consistent URL structure and can improve SEO by preventing duplicate content issues.
Table of Contents
Before You Begin
The .htaccess file is typically 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 of the file in case you need to restore the previous configuration.
Redirect WWW to Non-WWW
Add the following rules to your .htaccess file:
# Redirect www to non-www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L] How It Works
RewriteEngine Onenables URL rewriting.RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]checks whether the requested hostname starts withwww.RewriteRuleredirects the visitor to the same URL without thewwwprefix.R=301creates a permanent redirect.
Redirect WWW to Non-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://%1/$1 [R=301,L] Example:
https://www.yourdomain.com/page becomes:
https://yourdomain.com/page Testing the Redirect
After saving the .htaccess file:
- Open your website using the WWW version.
- Verify that the browser automatically redirects to the non-WWW address.
- Check several pages and subpages to ensure the redirect works correctly throughout the website.
Common Issues
If the redirect does not work:
- 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 non-WWW version of your website, add the following rule to the .htaccess file located in your website’s root directory:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L] If your website uses SSL, replace http:// with https:// to ensure visitors are redirected to the secure version of the site.