【问题标题】:301 Redirect with URL variables使用 URL 变量进行 301 重定向
【发布时间】:2020-02-14 05:14:00
【问题描述】:

我正在尝试进行 301 重定向。但它不会被重定向。

我的旧网址:https://www.example.com/category.php?filter=Short%20Term%20Renting

我的新网址:https://www.example.com/services/short-term-renting

我在 .htaccess 中的代码:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^([^&]&)*filter=Short%20Term%20Renting(&|$)
RewriteRule ^category.php/services$ /short-term-renting? [L,R=301]

我用这个链接“301 Redirecting URLs based on GET variables in .htaccess”引用了

【问题讨论】:

标签: .htaccess redirect


【解决方案1】:

您正在匹配 ^category.php/services,而 services 不存在于您当前的请求 URI 中。另外,无论字符大小写如何,我都建议匹配查询字符串。因此,您的 .htaccess 应该如下所示:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^.*?filter=Short%20Term%20Renting$ [NC]
RewriteRule ^category.php$ /services/short-term-renting? [NC,L,R=301]

演示: https://htaccess.madewithlove.be?share=8acfa0a0-cf9e-5036-a245-a5a24bd90b26

更新:

如果您想提供services/* URL,但向同一category.php 发出内部请求,您可以执行以下操作:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^.*?filter=Short%20Term%20Renting$ [NC]
RewriteRule ^category.php$ /services/short-term-renting? [L,R=301]
RewriteRule ^services\/(.*)$ /category.php?filter=$1 [L]

演示: https://htaccess.madewithlove.be?share=8f82a119-5cc9-5851-a3fa-ad9171ef6487

【讨论】:

  • 好的,它工作正常。但我需要像这里一样的 301 重定向。这样当我重定向时,只有 URL 的名称必须更改。网页内容必须一致。
猜你喜欢
  • 2016-05-30
  • 1970-01-01
  • 1970-01-01
  • 2012-05-14
  • 2018-02-25
  • 1970-01-01
  • 1970-01-01
  • 2021-01-06
  • 1970-01-01
相关资源
最近更新 更多