【问题标题】:Mod rewrite location header contains root file pathMod重写位置标头包含根文件路径
【发布时间】:2020-09-26 05:27:52
【问题描述】:

我正在尝试重写

https://example.com/vehicles.php?id=42https://example.com/vehicles/brand/honda

在不影响 SEO 的情况下处理我的新 URL 更改。 (即“42”应该映射到“honda”)

我试过this

RewriteCond  %{QUERY_STRING}  ^id=42$                   
RewriteRule  ^vehicles.php$  ^vehicles/brand/honda$    [L,R=301]   #to handle old URLs to new URL mapping
RewriteRule  ^vehicles/brand/honda$   ^vehicles.php?id=42$   [L]  #to handle URL to file mapping

但它会重定向到

https://example.com/root/site/%5evehicles/brand/honda$?id=42 有两个错误。

  1. 它包含一个文件路径 (/root/site)
  2. 我希望删除查询参数。

感谢一些帮助以解决此问题。

【问题讨论】:

  • 你的 RewriteRule 是倒置的,应该是 RewriteRule ^vehicles/brand/honda$ vehicles.php$
  • 该重定向已经存在于另一个规则中。我只想将我的旧 SEOed URL(例如 https://example.com/vehicles.php?id=42)重定向到新的 URL 方案,即 https://example.com/vehicles/brand/honda。清楚吗?我更新了上面的规则以显示两者。
  • 为什么要在 substitution URL 中使用正则表达式锚定语法?这根本没有意义。 ^$ 绝对没有生意。
  • 没错。我从这篇博文中得到它。 smashingmagazine.com/2011/11/introduction-to-url-rewriting 但从中删除了正则表达式部分。

标签: php .htaccess mod-rewrite query-string


【解决方案1】:

行尾的字符? 从输出路径中删除查询字符串。

RewriteCond %{QUERY_STRING} ^id=42$
RewriteRule  ^vehicles.php$  vehicles/brand/honda?    [L,R=301]

【讨论】:

  • 太棒了。这解决了上面的问题2。谢谢你。对问题 1 有任何想法吗?
  • 我认为它与 .htaccess 文件无关,但是您可以随时输入 RewriteRule ^vehicles.php$ http://example.com/vehicles/brand/honda? [L,R=301]
  • 谢谢,我会采用那个解决方案,直到找到根本原因。
猜你喜欢
  • 1970-01-01
  • 2015-12-06
  • 2014-10-21
  • 2021-07-11
  • 1970-01-01
  • 2012-08-04
  • 2023-03-29
  • 2012-12-16
  • 2010-12-17
相关资源
最近更新 更多