【问题标题】:How to redirect users to new url on same site with .htaccess?如何使用.htaccess 将用户重定向到同一站点上的新网址?
【发布时间】:2011-12-28 17:28:26
【问题描述】:

我最近更改了我网站的网址。

旧网址:mysite.com/top/1

新网址:mysite.com/top/page=1

当用户访问旧网址时,我想将他们重定向到新网址。我如何使用 .htaccess 来做到这一点?

我有这个新网址的重写规则

RewriteRule ([top,new]+)/page=([0-9]+) index.php?h=$1&page=$2 [L]

【问题讨论】:

  • 您确定新网址吗?你是说mysite.com/top/?page=1page前面的问号)吗?
  • 不,我现在更新了问题检查

标签: .htaccess mod-rewrite redirect


【解决方案1】:

试试这个

RewriteEngine On
RewriteRule ^top/([0-9]+)$ top/page=$1 [R=301,L]

【讨论】:

  • @Virendra 你的重写规则会导致无限重写:)
  • 我已经有了新网址RewriteRule ([top,new]+)/page=([0-9]+) index.php?h=$1&page=$2 [L]的重写规则
  • 是的,它会导致无限次重写。让我试试别的。
  • @RishikaSinghania 你应该更新你想要什么的问题。
  • 我已根据您在问题中的要求更新了解决方案,即将mysite.com/top/1 重定向到mysite.com/top/page=1。但是,您确定要以这种方式重定向它,而不是像mysite.com/top/?page=1
【解决方案2】:

对于您最初的问题,您应该尝试以下方法:

RewriteRule ^top/([0-9]+)$ top/page=$1

但根据您对 Virendra 帖子的评论,这可能会更好地满足您的需求:

RewriteRule ^(top|new)/([0-9]+)$ index.php?h=$1&page=$2 [R=301,L]

这将匹配 top 和 new,并且仅在指定有效页码时重写。

希望对你有帮助

【讨论】:

  • 这不会按照问题的要求重定向到mysite.com/top/page=1
  • 根据他对您的回答的评论,他想要这种行为而不是他在问题中提出的行为,但我将编辑我的答案以包含两种解决方案
  • @RishikaSinghania 你试过哪个?
  • 服务器内部错误是因为flags中有空格。将其更改为[R=301,L]
  • 它说“页面没有正确重定向”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-07
  • 1970-01-01
  • 2021-12-07
  • 1970-01-01
相关资源
最近更新 更多