【问题标题】:Redirect php links with .htaccess使用 .htaccess 重定向 php 链接
【发布时间】:2018-07-15 19:06:04
【问题描述】:

我有一个使用 php 创建的带有视频播放器的网站。 我已经设法清理了来自www.website.com/player.php?id=10 的 URL 到www.website.com/player/10,其中10 是给定视频的ID。

.htaccess 文件的代码如下:

RewriteEngine On
RewriteRule ^player/([0-9]+)$ player.php?id=$1

如果我直接输入www.website.com/player/10,这会很好,但是,如果有人试图访问旧链接,他们不会被重定向。如何在 .htaccess 文件中执行此操作?

【问题讨论】:

  • 重写不会重定向到新的 URL。它只需要一个匹配的 URL 并在内部调用另一个 URL。为了更好地控制它,我建议您考虑使用一些路由器。
  • 您可以使用.htaccess 为原始player.php URL 设置另一个重定向规则。 This Q&A may help.

标签: php .htaccess redirect


【解决方案1】:

重定向重写是有区别的。

重定向 向客户端发出一个 HTTP 标头,强制他们请求新的位置。 重写在服务器内部,并将请求的位置映射到不同的位置。

所以,只需将“old”样式重定向到“new”样式并重写“new”样式:

# Match id in the query string and capture the id
RewriteCond %{QUERY_STRING}     ^id=(.*)$         [NC]
# Redirect the old to the new player/id
RewriteRule ^player.php$        player/%1         [NC,R=301,L]
# Rewrite the new to the old
RewriteRule ^player/([0-9]+)$   player.php?id=$1

【讨论】:

  • 嗯.. 我不认为你可以在你的第一个重写规则中访问 get 参数。
  • @ChinLeung:你可能是对的。这样看起来更好吗?
  • 您的规则将循环,因为您的第二条规则的 player.php?id=$1 也匹配第一个规则的 Rewrite 模式和 RewriteCond。为了防止这个循环,你需要匹配%{THE_REQUEST}变量而不是%{QUERY_STRING}
  • @starkeen:你必须更具体地说明什么匹配什么,我看不出来。
  • 看到这个类似的帖子stackoverflow.com/questions/21578126/…
猜你喜欢
  • 1970-01-01
  • 2017-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多