Learn how to redirect non-www to www URLs for your website. This step-by-step guide ensures your site remains accessible and SEO-friendly by implementing the correct URL redirection. Perfect for webmasters and SEO enthusiasts looking to optimize site structure.
Introduction
When setting up a website, one of the critical decisions you’ll make is whether to use a “www” or “non-www” version of your domain. While both options are technically valid, consistency is crucial for SEO and user experience. To avoid potential issues with duplicate content and to establish a uniform web presence, many webmasters choose to redirect non-www to www URLs. This guide will walk you through the process of setting up this redirection, ensuring your site remains SEO-friendly and accessible to all users.
Why Redirect Non-www to www URLs?
Before diving into the “how-to,” it’s essential to understand the “why.” Redirecting non-www to www URLs offers several benefits:
- SEO Consistency: Search engines may treat www and non-www versions as separate entities. Redirecting non-www to www ensures that all traffic is consolidated under a single domain, preventing duplicate content issues.
- Improved User Experience: Visitors will always land on the same version of your site, regardless of how they type the URL, creating a consistent and professional appearance.
- Better Analytics: Redirecting non-www to www simplifies your analytics by consolidating traffic data, giving you a clearer picture of your site’s performance.
Step 1: Choose Your Preferred Domain
Before setting up the redirection, decide which version of your domain you want to use—www or non-www. For this guide, we’ll assume you’ve chosen to redirect non-www to www URLs.
Step 2: Redirect Non-www to www Using .htaccess
If your website is hosted on an Apache server, the .htaccess file is the easiest way to set up a redirection. This file allows you to configure rules for your site, including URL redirections. Follow these steps to edit your .htaccess file:
1. Access Your .htaccess File
- Use an FTP client (like FileZilla) or your hosting provider’s file manager to access your site’s root directory.
- Locate the
.htaccess
file. If it doesn’t exist, you can create one using a plain text editor like Notepad.
2. Add the Redirection Code
- Open the
.htaccess
file and add the following code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
- Replace
yourdomain.com
with your actual domain name. - Save the file and upload it back to your server.
Or the solution outlined below
# Redirect to www
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
UPD.: for domains like .co.uk
, replace
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
with
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+\.[^.]+$
If you need to support http and https and preserve the protocol choice try the following:
RewriteRule ^login\$ https://www.%{HTTP_HOST}/login [R=301,L]
3. Test the Redirection
- After implementing the code, test the redirection by typing your non-www domain (e.g.,
http://yourdomain.com
) into a web browser. It should automatically redirect to the www version (e.g.,http://www.yourdomain.com
).
Step 3: Redirect Non-www to www Using cPanel
For those using cPanel, there’s a straightforward way to set up a redirection without manually editing the .htaccess file. Here’s how:
1. Log in to cPanel
- Access your hosting account’s cPanel.
2. Navigate to Redirects
- Under the “Domains” section, find and click on “Redirects.”
3. Set Up the Redirection
- Choose “Permanent (301)” as the redirection type.
- In the “https?://(www.)?” field, select your domain.
- In the “Redirects to” field, enter the full www version of your domain, including
http://
orhttps://
. - Ensure that the “www. redirection” option is checked.
- Click “Add” to save your changes.
4. Test the Redirection
- Like before, test the redirection by typing your non-www domain into a browser to confirm it redirects to the www version.
Step 4: Redirect Non-www to www Using Nginx
If your website is hosted on an Nginx server, you’ll need to modify the server configuration to set up the redirection. Here’s how:
1. Access Your Nginx Configuration File
- Use an SSH client to connect to your server.
- Locate and open your Nginx configuration file, typically found at
/etc/nginx/nginx.conf
or/etc/nginx/sites-available/default
.
2. Add the Redirection Code
- Add the following code to your server block:
server {
listen 80;
server_name yourdomain.com;
return 301 http://www.yourdomain.com$request_uri;
}
- Replace
yourdomain.com
with your actual domain name. - Save the changes and exit the text editor.
3. Restart Nginx
- For the changes to take effect, restart Nginx using the command:
sudo systemctl restart nginx
4. Test the Redirection
- Test the redirection by entering your non-www domain in a browser to ensure it redirects correctly to the www version.
Step 5: Verify the Redirection Using SEO Tools
After setting up the redirection, it’s essential to verify that it’s working correctly. You can use various SEO tools to check if your non-www URLs are properly redirecting to the www version:
1. Online Redirect Checker
- Use online tools like Redirect Checker to confirm that the 301 redirection is set up correctly.
2. Google Search Console
- Ensure that you’ve added both versions of your domain (www and non-www) to Google Search Console. You can use the “URL Inspection” tool to check the status of individual URLs and confirm the redirection.
3. Browser Developer Tools
- Use the “Network” tab in your browser’s developer tools to inspect the HTTP response when accessing your non-www domain. Look for a 301 status code indicating a successful redirection.
Step 6: Update Google Analytics and Search Console
If you’ve implemented the redirection, it’s essential to update your settings in Google Analytics and Google Search Console to reflect the preferred domain:
1. Google Analytics
- In Google Analytics, go to “Admin” > “Property Settings” and update the default URL to the www version.
2. Google Search Console
- In Google Search Console, set the preferred domain to the www version. This ensures that Google understands which version of your site to prioritize in search results.
Conclusion
Redirecting non-www to www URLs is a crucial step in maintaining a consistent and SEO-friendly website. Whether you’re using Apache, cPanel, or Nginx, the methods outlined in this guide make it easy to set up the redirection and ensure your site remains accessible and optimized for search engines. By following these steps, you can avoid duplicate content issues, improve user experience, and maintain a strong web presence.
FAQs
- What is the difference between 301 and 302 redirections?
- A 301 redirection is permanent, indicating that the URL has been moved permanently, while a 302 redirection is temporary.
- Will redirecting non-www to www affect my SEO?
- Yes, it will positively impact your SEO by consolidating traffic and avoiding duplicate content issues.