大网数据广告活动
网站运营与故障问答

Preventing URLs ending in "/" from search engine indexes with robots.txt or rewrite rule

· 52CE 我爱测

Is there a programmatic way to easily deal with the issue of search engines considering pages where the page works with and without the trailing slash duplicates like via a robots.txt or Nginx rewrite rule? Examples of "duplicate…

问题原文

原帖链接 · Realto619 · CC BY-SA 4.0

Is there a programmatic way to easily deal with the issue of search engines considering pages where the page works with and without the trailing slash duplicates like via a robots.txt or Nginx rewrite rule?

Examples of "duplicate content" according to search engines:

https://www.jumpingthruhoops.com/filepath
https://www.jumpingthruhoops.com/filepath/

Maybe something like this for the rewrite rule, except I'm not sure what to use as far as where to redirect to (hence the question mark below)

rewrite ^/$ ? permanent;

We use Magento 1.13, where some content already has canonical links generated automatically. So if either the robots or rewrite rules won't work, I'll probably have to try to find an extension that performs that task on pages that don't already have canonical links. Or if anyone has a better approach, I'd be very interested in hearing about it.

社区高分解答

原帖链接 · DocRoot · CC BY-SA 4.0

rewrite ^/$ ? permanent;

This would only match the document root (a valid request) - this isn't a URL that ends with a slash, but a URL that starts with a slash.

Try something like the following instead, to match any request URL that ends with a slash:

rewrite ^(.+)/$ $1 redirect;

$1 is a backreference to the original URL-path, less the trailing slash.

Change the redirect flag to permanent once you have tested that it works OK - to avoid any caching issues.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2018-05-24)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。