【发布时间】:2020-09-26 05:27:52
【问题描述】:
我正在尝试重写
https://example.com/vehicles.php?id=42 到 https://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 有两个错误。
- 它包含一个文件路径 (
/root/site) - 我希望删除查询参数。
感谢一些帮助以解决此问题。
【问题讨论】:
-
你的 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