Understanding the Importance of .htaccess for Canonical URLs
When managing a website, one key aspect of SEO and user experience is ensuring that your site has a canonical URL. The canonical URL is the primary address for your site that search engines and browsers recognize as authoritative. Without specifying this, your site might be accessible through multiple URLs, such as:
http://example.comhttps://example.comhttp://www.example.comhttps://www.example.com
Search engines may interpret these as separate websites, which can dilute your SEO efforts and create duplicate content issues. Additionally, inconsistency can confuse users and weaken your site’s branding.
Using an .htaccess file, you can enforce a single, canonical version of your site’s URL. This configuration ensures that all traffic is redirected to the correct version, consolidating your SEO authority and improving user experience.
Benefits of Using .htaccess for URL Canonicalization
- Improved SEO: Search engines will know which URL to index and rank, eliminating duplicate content issues.
- Enhanced User Experience: Visitors are automatically directed to the secure, canonical version of your site.
- Consistent Branding: A single URL version reinforces your brand identity.
- Security: Redirecting to the
https://version ensures data is transmitted securely.
Configuring .htaccess to Redirect to an HTTPS Non-WWW URL
We’ve previously written about how to redirect to HTTPS and WWW. Click on the link to see that, if you do wish to have the www prefix appear in all of your URLs. Otherwise, continue on for instructions about how to redirect to HTTPS and an address that does not contain the www prefix.
Follow these steps to configure your .htaccess file to redirect all traffic to the https://example.com version of your site:
Step 1: Backup Your Existing .htaccess File
This is essential. Don’t blame us if you skip over this step, as each site is configured slightly differently. Before making any changes, create a backup of your existing .htaccess file to prevent accidental loss of your original configurations. Also, do not over-write any special settings that already exist in your .htaccess file. It’s possible that your host or your CMS, such as WordPress, may have already added settings to your .htaccess file. If that’s the case, you’ll usually want to keep those in place and simply to add the following redirect rules without modifying anything else. If by chance you already have similar but conflicting
Step 2: Add the Redirect Rules
Edit your .htaccess file (usually located in the root directory of your site) and add the following code:
# Redirect to HTTPS non-WWW version
# Force HTTPS + non-WWW (canonical)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=301]
</IfModule>
Explanation of the Code:
<IfModule mod_rewrite.c>: Ensures the rewrite module is enabled before applying rules.RewriteEngine On: Activates the rewrite engine.RewriteCond %{HTTPS} !=on [OR]: Checks if the current request is not using HTTPS.RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]: Checks if the URL starts withwww.(case-insensitive).RewriteRule ^ https://example.com%{REQUEST_URI}: Redirects to thehttps://version withoutwwwwhile preserving the requested URI. The[L,R=301]flags specify a permanent redirect. The redirect target is hard-coded to the canonical hostname, which prevents accidental redirects to an empty or malformed hostname. This reduces the risk of 500 errors and makes the redirect logic easier to understand.
Importantly, change example.com to your own domain name, or you’ll be redirecting somewhere else entirely.
Step 3: Save and Test
- Save the
.htaccessfile. - Clear your browser cache and test various versions of your site’s URL (e.g.,
http://www.example.com,https://www.example.com). Each should redirect tohttps://example.com. - If anything goes wrong, revert to your original, backed-up version of the file while you troubleshoot.
Tips & Troubleshooting
- Test with 302 first, then switch to 301: Browsers cache 301s aggressively, so a small mistake can look like it “won’t go away” even after you fix it.
- If using WordPress to power your site: Place your canonical redirect rule above the WordPress
# BEGIN WordPressblock so WordPress does not intercept the request first. - If your site is behind a proxy/CDN,
%{HTTPS}might not reflect reality: Some setups terminate SSL at the proxy, so Apache sees HTTP. In those cases, you may need to key offX-Forwarded-Protoinstead (or use your host/CDN’s recommended method). - 500 Internal Server Error: This typically occurs due to syntax errors. Double-check your
.htaccessfile for typos or misplaced directives. - Redirect Loop: Ensure there are no conflicting redirects in other configuration files, or elsewhere in the same .htaccess file.
- Changes Not Taking Effect: Verify that
.htaccessfiles are enabled on your server. Check your server’s Apache configuration forAllowOverride Allin the relevant directory block.
Final Thoughts
A well-configured .htaccess file not only resolves canonical URL issues but also enhances the overall user experience and SEO of your website. Taking the time to enforce a single URL format helps search engines understand your site better, consolidates your traffic, and strengthens your online presence. Good luck!
Last modified: December 17, 2025
One Reply to “Using .htaccess to redirect to HTTPS and non-WWW”
Leave a Reply
You must be logged in to post a comment.
for non-www to www URLs or to redirect HTTP to HTTPS using the .htaccess file. This is useful for ensuring your site maintains consistent URLs and improves SEO. The instructions walk through the process step by step