正确重定向以避免 www/non-www 页面被计为重复页面

Proper redirection to avoid www/non-www pages being counted as duplicates

站点审核(Semrush、Seobility)指出我们最大的问题之一是重复内容,指出 "This website uses URLs with www and non-www subdomain. This may cause duplicate content and bad links to your website."

所以,基本上 www.oursite.com/about 和我们的网站。com/about 被算作重复:

该站点是使用 Slim 框架 PHP 构建的。中间件处理整个站点中 https 的使用。另外,目前我们有一个.htaccess如下:

RewriteCond %{HTTP_HOST} ^(www\.)?oursite\.com$ [NC]
RewriteRule ^(?!public)(.*)$ /public/ [L]

此代码通过 public 文件夹管理所有页面的路由。

我们如何编辑此代码以避免 www 和非 www 地址被计为重复地址?

Our preference would be without www.

假设您没有链接到 HTML 源中的绝对 URLs(需要更新),那么您可以在 before 你现有的重写:

RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) https://example.com/ [R=301,L]

如果请求的主机启动 www. 则重定向到规范 URL。

A middleware handles the use of https across the whole site.

请注意,这可能会在请求 http://www.example.com/ (HTTP + www) 时导致 2 次重定向。但这不一定是坏事。