我正在尝试执行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”引荐
答案 0 :(得分: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/*
网址,但又向同一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