How to Redirect Non-www URLs to www Using .htaccess
Overview
This guide will show you how to redirect visitors from your non-www domain (example.com) to your www version (www.example.com) using an .htaccess file. This ensures consistent URL structure and can help with SEO by preventing duplicate content issues.
What is URL Redirection?
URL redirection automatically sends visitors from one web address to another. When someone types "yoursite.com" in their browser, they'll be automatically redirected to "www.yoursite.com" instead.
Benefits of www Redirection
- SEO Consistency: Search engines treat www and non-www as separate sites
- Analytics Accuracy: Prevents traffic data from being split between two versions
- Professional Appearance: Maintains consistent branding and URL structure
- Cookie Management: Ensures cookies work properly across your entire domain
Accessing Your Website Files
Before editing or creating an .htaccess file, you need to access your website's files. You can do this through two methods:
Method 1: Using cPanel File Manager
Accessing cPanel:
Option A - Through Client Portal:
- Visit: https://ifastnet.com/portal/clientarea.php
- Log in with your hosting account credentials
- Navigate to your hosting services
- Click on the cPanel access button
Option B - Direct cPanel Access:
- Visit: https://yourdomain.com/cpanel (replace "yourdomain.com" with your actual domain)
- Enter your cPanel username and password
Using File Manager:
- Once in cPanel, locate and click File Manager
- Navigate to your website's root directory (usually
public_html)
- Important: Enable "Show Hidden Files" to see existing .htaccess files
- Click Settings in the top-right corner of File Manager
- Check the box for Show Hidden Files (dotfiles)
- Click Save
Method 2: Using FTP
- Connect to your hosting account using an FTP client (like FileZilla)
- Navigate to your website's root directory (usually
public_html)
- Look for existing .htaccess files (they may be hidden by default)
Checking for Existing .htaccess File
In File Manager:
- After enabling "Show Hidden Files," look for a file named
.htaccess
- If you see one, you'll need to edit it
- If no .htaccess file exists, you'll need to create a new one
In FTP:
- Enable showing hidden files in your FTP client settings
- Look for
.htaccess in the root directory
- Download it for editing if it exists
Creating or Editing .htaccess File
Option 1: Creating a New .htaccess File
Using File Manager:
- In your website's root directory (
public_html)
- Click + File to create a new file
- Name the file exactly:
.htaccess (include the dot at the beginning)
- Click Create New File
Using FTP:
- Create a new text file on your computer
- Name it
.htaccess
- Edit the file and upload it to your root directory
Option 2: Editing an Existing .htaccess File
Using File Manager:
- Right-click on the existing
.htaccess file
- Select Edit from the context menu
- The file will open in the built-in editor
Using FTP:
- Download the existing
.htaccess file to your computer
- Open it with a text editor (like Notepad)
- Make your changes and upload it back
Adding the Redirect Code
Add the following code to your .htaccess file to redirect non-www to www:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R=301,L]
Important: Replace "yourdomain.com" with your actual domain name.
Example for a Domain Called "mywebsite.com":
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mywebsite\.com$ [NC]
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R=301,L]
If You Already Have Content in .htaccess:
- Add the redirect code at the top of the file
- Leave existing code below the redirect rules
- Ensure there are no conflicting rules
Code Explanation
- RewriteEngine On: Enables URL rewriting functionality
- RewriteCond: Sets the condition (when domain doesn't have www)
- RewriteRule: Defines the redirect action
- R=301: Creates a permanent redirect (good for SEO)
- L: Stops processing additional rules after this one matches
Saving Your Changes
Using File Manager:
- After adding the code, click Save Changes
- Close the editor
- Your changes are immediately active
Using FTP:
- Save the file on your computer
- Upload it to your website's root directory
- Overwrite the existing file if prompted
Testing Your Redirect
Manual Testing:
- Open a web browser
- Type your domain without www (e.g.,
yoursite.com)
- Press Enter
- Verify that it redirects to the www version (
www.yoursite.com)
- Check that the URL in the address bar shows www
Testing Different Pages:
- Test with specific pages:
yoursite.com/about should redirect to www.yoursite.com/about
- Ensure all internal links work properly
- Verify that contact forms and other functionality still work
Troubleshooting Common Issues
Redirect Not Working:
- Check file location: Ensure .htaccess is in the root directory (
public_html)
- Verify file name: Must be exactly
.htaccess (with the dot)
- Check syntax: Ensure code is copied correctly without extra spaces
- Clear browser cache: Use Ctrl+F5 to force refresh
Website Not Loading:
- Syntax error: Check that all code is copied correctly
- File permissions: Ensure .htaccess has proper permissions (644)
- Backup restore: If needed, restore from backup or delete .htaccess temporarily
Infinite Redirect Loop:
- Check existing rules: Look for conflicting redirect rules
- Domain spelling: Verify domain name is spelled correctly in the code
- Protocol conflicts: Ensure HTTP/HTTPS settings are consistent
Advanced Considerations
HTTPS Redirects:
If your website uses SSL, the redirect code above includes HTTPS. If you need HTTP only:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]
Multiple Domain Redirects:
For multiple domains pointing to the same site:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(yourdomain\.com|anotherdomain\.com)$ [NC]
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R=301,L]
File Backup and Safety
Before Making Changes:
- Download a backup of your existing .htaccess file
- Note current website functionality to verify after changes
- Test during low-traffic periods when possible
If Something Goes Wrong:
- Restore your backup .htaccess file
- Clear your browser cache
- Contact support if issues persist
Getting Support
If you encounter problems or need assistance with .htaccess redirects:
Support Portal Access:
- Visit: https://support.ifastnet.com/login.php
- First-time users: You'll need to register for a support account before creating tickets
- Create a detailed support ticket describing your issue
What to Include in Your Support Ticket:
- Your domain name
- Description of the redirect you're trying to implement
- Any error messages you're seeing
- Steps you've already tried
- A copy of your .htaccess file content (if relevant)
Best Practices
SEO Considerations:
- Use 301 (permanent) redirects for SEO benefits
- Implement redirects consistently across your entire site
- Update internal links to use your preferred URL format
- Submit your preferred domain format to search engines
Maintenance Tips:
- Regularly test your redirects to ensure they're working
- Keep backups of your .htaccess file
- Document any custom rules you add
- Monitor website performance after implementing redirects
Alternative Methods
While .htaccess is the most common method, other options include:
- DNS-level redirects (through your domain registrar)
- Server-level configuration (requires server access)
- Plugin-based redirects (for CMS like WordPress)
- Application-level redirects (in your website code)
Summary
Setting up non-www to www redirects using .htaccess is a straightforward process that involves creating or editing a simple text file in your website's root directory. Always backup your existing .htaccess file before making changes, test thoroughly after implementation, and don't hesitate to contact support if you encounter any issues. This redirect will help maintain consistent URLs and improve your website's SEO performance.