【问题标题】:How can I write a .htaccess redirect to find all numbers?如何编写 .htaccess 重定向来查找所有数字?
【发布时间】:2012-09-03 14:05:45
【问题描述】:

我正在尝试找出一个正则表达式,它会在 URL 中找到一些数字并在我重定向到的 URL 中使用它们。

Redirect 301 /page.php?id=95485666 http://test.com/profile/info/id/95485666

我在想也许

Redirect 301 /page.php?id=([0-9]+) http://test.com/profile/info/id/$1

但它似乎不起作用

另外,如果我执行 301 重定向,我必须将代码保留在 .htaccess 文件中多长时间? Google 什么时候才能确定新链接是好链接?

【问题讨论】:

  • 您应该从“Also...”中提取所有内容并将其作为一个单独的问题。尝试将多个问题挤成一个问题并不是一个好主意。

标签: php regex .htaccess numbers hyperlink


【解决方案1】:

好吧,我猜语法不正确。试试这个:

RewriteRule ^page.php?id=([0-9]+)  http://test.com/profile/info/id/$1  [R=301,L]

【讨论】:

  • 您无法匹配RewriteRule中的查询字符串
【解决方案2】:

您不能匹配 Redirect 指令中的查询字符串(也不能在 RedirectMatch/RewriteRule 中)。你需要使用mod_rewrite的%{QUERY_STRING}var:

RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)id=([0-9]+)($|&)
RewriteRule ^/?page\.php$ http://test.com/profile/info/id/%2? [L,R=301]

【讨论】:

  • ? 添加到重写 URL 的末尾以丢弃原始查询字符串。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-07
相关资源
最近更新 更多