【问题标题】:Regex 301 redirects正则表达式 301 重定向
【发布时间】:2018-04-19 23:27:54
【问题描述】:

使用Wordpress/Woocommerce最新版本:

我需要 301 将我的旧商店网址重定向到我的新网站,该网站有更清晰的网址,但有超过 2000 个网址,我希望尽可能多地自动执行此操作。希望 .htaccess 中的正则表达式模式可以做到这一点?我对如何做到这一点的了解非常有限,因此非常感谢任何帮助。

旧网址格式

foobar.com/vintage-industrial/3135-sgabello-alto-cindy-industrial-arredamento-industrial-sgabello-.html
foobar.com/shabby-e-provenzale/3270-cubo-cottage-2-cassetti-arredamento-shabby-chic-etnico-nordico.html
foobar.com/mobili-etnici/3286-armadio-lambert-blue-arredamento-etnico-classico.html
foobar.com/mobili-etnici/3335-tavolo-telgede-80-tavolo-rustico-industrial-arredamento-etnico-arredamento-industrial.html
foobar.com/sedie-sgabelli/1687-sedia-jenny-arancio-sedie-colorate-pop-vintage.html

上面的例子显示了一个目录,可以是任何东西,后面是产品id和名称

新的网址格式

foobar.com/prodotto/sgabello-alto-cindy
foobar.com/prodotto/cubo-cottage-2
foobar.com/prodotto/armadio-lambert-blue
foobar.com/prodotto/tavolo-telgede-80-tavolo
foobar.com/prodotto/sedia-jenny-arancio

每个新网址都将采用这种简单的格式,并且所有网址都将具有“prodotto”基本网址

旧的url里面塞了很多关键词,新的url已经去掉了

更新

有什么办法可以去掉这部分旧网址:

'vintage-industrial/3135-'

我认为这就是我可能需要做的所有事情,因为 mod_rewrite 应该从 url 的其余部分找到产品?所以我会留下

foobar.com/sgabello-alto-cindy-industrial-arredamento-industrial-sgabello

谢谢

【问题讨论】:

  • 你能再举几个例子来说明清楚吗?

标签: wordpress .htaccess woocommerce url-redirection


【解决方案1】:

所以基本上我们有:

foobar.com/type/####-product-name-and-key-words.html

将被翻译成

foobar.com/prodotto/product-name

如果您只有几个类型,我们可以这样做:

RewriteRule "^/first-type/[0-9]+-(.*)\.html$" "/prodotto/$1"
RewriteRule "^/second-type/[0-9]+-(.*)\.html$" "/prodotto/$1"\

这更安全,但如果你有很多,那就有点乏味了。我们也可以这样做:

RewriteRule "^/.*/[0-9]+-(.*)\.html$" "/prodotto/$1"

这样更快,但匹配我们不想要的东西的风险很小。无论哪种方式,我们都会留下foobar.com/prodotto/product-name-and-key-words

我认为这就是我可能需要做的所有事情,因为 mod_rewrite 应该从 url 的其余部分找到产品?

如果是这样,那就太好了!我们完成了。如果没有,我们将不得不继续。

如果您没有一个易于遵循的规则来将名称与关键字分开,那么我们将不得不增加一点创意。我们可以使用我们自己的代码override WP's 404 page。这个 404 页面会做一些很奇怪的事情。它将剥离其 url 的最后一部分(基于破折号/斜线)并重定向到削减的 url。这样我们将逐步接近正确的页面:

foobar.com/type/####-product-name-and-key-words.html
foobar.com/prodotto/product-name-and-key-words
foobar.com/prodotto/product-name-and-key
foobar.com/prodotto/product-name-and
foobar.com/prodotto/product-name

这具有大量重定向的负面影响,但它们的浏览器应该学习永久化。这也意味着您实际上不能拥有 404 页面,因为它会逐步剥离 url,直到您到达您的主页。

【讨论】:

  • 感谢 Kallmanation 的回复,我将尝试您的第一个解决方案,因为我认为我们只有少数类别(我认为少于 10 个),所以这可以工作!
  • 您的第一种方法让我对如何实现这一目标有了一个很好的了解,所以我会在有更新时报告!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-31
  • 2020-10-14
  • 2012-06-21
  • 2017-06-21
  • 2014-12-05
  • 2015-08-29
相关资源
最近更新 更多