【问题标题】:Mass 301 redirect using .htaccess使用 .htaccess 进行大规模 301 重定向
【发布时间】:2011-03-21 16:55:50
【问题描述】:

我正在从事一个大型项目,该项目涉及获取数千 (30,000+) 个静态网页并将其转换为 CMS。

问题是其中许多页面在其目录中是重复的。我想通过使用 301 重定向来保持 SEO 完好无损,但是,我不确定如何进行如此大的重定向 (301)。

这是页面当前目录结构的示例。

/page.html /文件夹/page.html /文件夹/子文件夹/page.html /文件夹/子文件夹/另一个文件夹/page.html

如您所见,page.html 在所有目录中都有重复。

对于新的 CMS,该页面的 URL 将是 /page.html

【问题讨论】:

标签: apache .htaccess redirect seo http-status-code-301


【解决方案1】:

工作示例,请访问:http://www.jakeisonline.com/stackoverflow/3345518-mass-301-redirect/page.html

您应该被直接重定向到 /page.html

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^(.*)page.html /page.html [R=301,NC]

这将始终使用 301 硬重定向将 http://www.foo.com/something/something/something/page.html 重定向回 http://www.foo.com/page.html

重写规则通过查看 URL 来执行此操作,确定是否包含 page.html 之前的任何内容(不包括域本身),如果是,将 301 重定向。所以你可以从字面上使用任何子级别,只要它以 page.html 结尾,它就会重定向到根目录下的 /page.html。

如果您想知道[R=301,NC] 的含义,

 R // means simple redirect
 R=301 // means redirect with a 301 header
 NC // means no-case, or case insensitive
 L // can also be used to say 'ignore all the other rules after this'

【讨论】:

    【解决方案2】:

    试试这个规则:

    RewriteRule ^([^/]+/)+page\.html$ /page.html [L,R=301]
    

    或者对于任意的page.html

    RewriteRule ^([^/]+/)+([^/]+)\.html$ /$2.html [L,R=301]
    

    【讨论】:

      猜你喜欢
      • 2017-04-15
      • 2015-08-15
      • 2014-02-03
      • 1970-01-01
      • 1970-01-01
      • 2010-12-22
      相关资源
      最近更新 更多