【问题标题】:.htaccess; 301 redirect not working.htaccess; 301重定向不起作用
【发布时间】:2017-06-30 20:16:12
【问题描述】:

我在.htaccess中尝试了以下代码到301重定向www.example.com/?content=file.php&id=16www.example.com/file/This-is-the-title/16

RewriteEngine on
Redirect 301 /?content=file.php&id=16 /file/This-is-the-title/16

但它没有重定向。 URL 保持原样。

我做错了什么?

附:我不是要求重写左右。我需要一个 301 重定向。

【问题讨论】:

  • 我的回答对你有用吗?或许你能接受?谢谢。
  • 我删除了您的编辑 David,除了替换 URL 上的错字取自您的问题(并且仍然存在 - 我会更新您的问题)。要通过它们,可以在 RewriteRule 中测试 REQUEST_URI,当它在 .htaccess 中时,^$ 与匹配 / 相同。 query_string 可以按照我的方式进行匹配,它不必是正则表达式。基本 URL http://www.example.com 在不更改时不必在替换中。
  • 我还根据需要将RewriteEngine on 放回原处,这对以后引用此内容的任何人都非常有用,即使您碰巧已经为其他规则设置了它,所以不需要它来反对这个一。这是为了让答案完整。

标签: .htaccess redirect url-redirection http-status-code-301


【解决方案1】:

Redirect 指令与查询字符串不匹配。改用这个:

RewriteEngine on
RewriteCond %{QUERY_STRING} =content=file.php&id=16
RewriteRule ^$ /file/This-is-the-title/16? [R=301,L]

【讨论】:

  • 您只需要在 RewriteRule substitution (或 Apache 2.4+ 上的 QSD 标志)末尾添加一个 ? 即可删除查询字符串来自重定向的 URL。
  • 谢谢,我总是忘记这些!也许我现在会记得 :)
猜你喜欢
  • 2013-04-06
  • 2014-07-05
  • 2014-12-21
  • 2011-03-08
  • 2013-03-14
  • 2023-03-27
  • 1970-01-01
相关资源
最近更新 更多