【问题标题】:wildcard 301 redirect to ignore variable in url通配符 301 重定向以忽略 url 中的变量
【发布时间】:2013-05-20 12:30:37
【问题描述】:

我不需要为我所有的旧产品 url 编写一个简单的重定向,而是需要一个重写规则来重定向几个具有非常相似 url 的链接,这些链接仅由一个用句点分隔的数字不同。下面的几个链接示例将全部重定向到一个 url。此外,“a131.1.html”中的字符大小写也应被忽略,而 i 仅用于文件名部分的目标 url。

site.com/product/cat/a131.1.html
site.com/product/cat/a131.2.html
site.com/product/cat/a131.3.html
site.com/product/cat/a131.4.html
...
site.com/product/cat/a131.30.html

to

new-site.com/101

【问题讨论】:

  • “101”从何而来?例如“a131.3”应该变成什么?

标签: apache .htaccess mod-rewrite


【解决方案1】:

类似的东西(因为我不清楚应该变成什么):

RewriteEngine On

RewriteRule ^product/cat/a([0-9]+)\.([0-9]+)\.html$ http://new-site.com/$1-$2 [L]

(我还没有尝试过,但希望它会起作用。在最坏的情况下,你现在已经知道要遵循的路线,你应该寻找什么)

【讨论】:

  • product/cat/a131.1.html 应该变成 newsite.com/b321 并且 product/cat/a131.2.html 应该变成 newsite.com/b321 等等。
  • @user2278150 所以最后一部分被忽略了? 321 是从哪里来的?
  • a131.1.html 和所有类似的旧产品 url 我想重定向到 newsite.com/b321 的新产品 url
  • 谢谢。现在工作。我能够使用您的原始帖子通过以下方式解决它,并查看标准重定向: RewriteCond %{HTTP_HOST} ^oldsite\.cin$ [OR] RewriteCond %{HTTP_HOST} ^www\.oldsite\.com$ RewriteRule ^pruducts\/cat/a131\.([0-9]+)\.html$ "http\:\/\/www\.newsite.com\.org\/B321" [R=301,L]
【解决方案2】:

您可以稍微缩小 .htaccess。

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.com$
RewriteRule ^product/cat/a131\.\d+\.html$ http://newsite.com/b321 [R=301,L]

【讨论】:

  • 是的,\d 代表所有数字,我想我可以压缩你的两个 HOST 条件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-27
  • 2013-10-19
  • 1970-01-01
  • 2020-09-25
  • 1970-01-01
  • 2018-11-25
  • 2015-11-04
相关资源
最近更新 更多