apache - 使用 .htaccess 进行大规模 301 重定向

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

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

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

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

/page.html
/folder/page.html
/folder/subfolder/page.html
/folder/subfolder/anotherfolder/page.html

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

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

最佳答案

工作示例,访问: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]

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

重写规则通过查看 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'

关于apache - 使用 .htaccess 进行大规模 301 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3345518/

相关文章:

php - 通过 mysql 数据库重定向多个 url?

python - Flask 发送流作为响应

apache - 如何在 Tomcat 6 中重写 URL

php - 允许 PHP 更改文件和目录的所有权和权限

apache - 无效命令 'RedirectMatch' - Apache 2.47。 Ubuntu 信任

apache - 使用apache检查请求中是否存在 header

apache - 动态.htaccess规则

java - 为什么 Tomcat 的 LifyCycleSupport.java 使用数组而不是任何高级容器(ArrayList)来存储监听器?

linux - 自己开发论坛软件好还是现成的论坛软件好?

.htaccess - 停止将查询字符串附加到 URL