Could we help you? Please click the banners. We are young and desperately need the money
Duplicate content can hurt your website’s visibility in search engine rankings. If search engines encounter multiple URLs that lead to the same content, it confuses them, which can negatively impact your SEO. Fortunately, using Apache’s .htaccess file and some clever rewrite rules, you can easily fix this problem. In this post, we’ll explain how a simple rewrite rule can help prevent duplicate content and enhance your website’s SEO.
Before diving into the technical details, it's important to understand why duplicate content is a major issue for SEO. Duplicate content refers to identical or very similar content accessible via different URLs. When this happens, search engines may have difficulty determining which version of the page to index or rank. This can lead to:
The solution is to enforce a consistent and secure URL structure using .htaccess rewrite rules. Let’s break down one of the most common and useful rules.
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example-page\.ch$ [NC]
RewriteRule ^(.*)$ https://www.example-page.ch/$1 [L,R=301]
This rule consists of two conditions and a rewrite action. Here’s a detailed explanation of each part:
RewriteCond %{HTTPS} off
This condition checks if the request is not using a secure HTTPS connection. If HTTPS is off, it means the request is being made over HTTP. Since serving content over HTTP and HTTPS can create duplicate content issues, you want to redirect all traffic to the HTTPS version.
RewriteCond %{HTTP_HOST} !^www\.example-page\.ch$
This condition checks whether the requested domain is not exactly www.example-page.ch
. The !^ indicates a negative match, meaning that if the host doesn't exactly match the www.example-page.ch domain, the rule should be triggered. The [NC] flag at the end makes the comparison case-insensitive.
RewriteRule ^(.*)$ https://www.example-page.ch/$1
If either of the above conditions (HTTP or non-www) is true, this RewriteRule triggers. It rewrites the URL to force it to use both HTTPS and the www prefix. The ^(.*)$ part captures the entire original request URI (like /about or /products/item/12), and $1 injects this original path after the canonical domain.
[L,R=301]
Now that we’ve broken down the rule, let’s connect it to the problem of duplicate content. Here’s how the rule helps:
If you’re using an Apache server, adding this rule to your website is straightforward. Follow these steps to implement it:
.htaccess
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example-page\.ch$ [NC]
RewriteRule ^(.*)$ https://www.example-page.ch/$1 [L,R=301]
Along with this rewrite rule, here are some additional SEO best practices for maintaining a clean and duplicate-free URL structure:
<link rel="canonical">
tags on your pages to declare a preferred URL to search engines.Using Apache’s .htaccess rewrite rules is an effective and efficient way to solve duplicate content issues. By forcing all traffic to HTTPS and using a consistent domain (with or without www), you help search engines correctly index your site while improving user experience. Implementing these simple rules can have a big impact on your site's SEO and ensure you avoid any penalties caused by duplicate content.
Don’t overlook these small technical SEO optimizations — they can make a big difference to how your site performs in search rankings.