重定向到具有相同文件名的不同文件夹

Redirect to a different folder with the same file name

我正在翻新网站,但我遇到了一个问题,即 URL 已被搜索引擎编入索引。 这些 URL 具有以下路径: https://www.myweb.com/products/product-1 https://www.myweb.com/products/product-2...

并且在新网站中路径更改为: https://www.myweb.com/items/product-1 https://www.myweb.com/items/product-2

product-1、product-2 仍保留名称,但将文件夹/products 更改为/items。 我如何在 htaccess 中执行操作,以便那些已编入索引的 URL 不会出现错误 404 并重定向到 /items 文件夹?

有什么想法吗?非常感谢您。

使用根 .htaccess 文件顶部的 mod_rewrite 尝试以下操作:

RewriteEngine On

RewriteRule ^products($|/.*) /items [R=301,L]

(如果 RewriteEngine 指令已经出现在文件中,则无需重复。)

301(永久)重定向确保保留旧 URL 的任何 SEO 值。搜索引擎将看到重定向并索引新的 URL。

但是,请先使用 302(临时)重定向进行测试,以避免潜在的缓存问题。

以上重定向:

  • /products/items
  • /products//items/
  • /products/<something>/items/<something>

但是重定向:

  • /products<something>