【问题标题】:.htaccess redirect using GET parameters.htaccess 使用 GET 参数重定向
【发布时间】:2016-03-18 09:13:36
【问题描述】:

我已经检查了很多与 .htaccess 重定向相关的答案,但我无法详细说明我的问题:

我有一个旧网站,其中的博客条目如下:

http://ellrohir.mzf.cz/index.php?page=blog&id=77

现在我还有一个带有“不错的网址”的新网站,可以使用以下方式找到博客条目:

http://alois-seckar.cz/politics-blog/77

我想“关闭”旧页面并将所有旧链接重定向到新网站。如何捕获旧网址并将其转移到新网址?我仍然想不出必要的 .htaccess 代码:(

【问题讨论】:

标签: php apache .htaccess mod-rewrite redirect


【解决方案1】:

试试这个:

RewriteEngine on


RewriteCond %{QUERY_STRING} ^page=([^&]+)&id=([^&]+)$ [NC]
RewriteRule ^index\.php$ http://alois-seckar.cz/politics-%1/%2? [NC,L,R]

末尾的空问号对于丢弃目标网址中的原始查询字符串很重要。

%n 是 RewriteCond 中正则表达式的一部分,它是在 ([ 和 ]+) 之间匹配的部分。

此规则将重定向

example.com/index.php?page=foo&id=bar

example2.com/politics-foo/bar

【讨论】:

  • 这个很棒!
【解决方案2】:

您可以使用.htaccess 重定向到另一个网站

RewriteEngine on
RewriteRule ^/index.php?page=blog&id=(.*)?$ http://example.com /politics-blog/$1 [R=301,L]

【讨论】:

  • 对我来说,这个没有做任何事情:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多