我正在尝试为 url 重写设置一些非常具体的条件,但我很吃力。
假设我有一个类似 http://www.example.com/?product=test&retailer=test&site=test
在这种情况下(其中存在 product
参数)我只想删除 &site=test
但保留 url 的其余部分不变。
如果 product
参数不存在,例如http://www.example.com/?retailer=test&site=test
我仍然想删除 &site=test
并将 ?retailer=test
更改为 /retailer/test
所以完整的 url 将是 http://www.example.com/retailer/test
。我也只希望这发生在根域上。甚至只是为了删除 site
参数真的很挣扎......
我试过这个:
RewriteCond %{QUERY_STRING} (?:^|&)sitechange=([^&]+) [NC]
RewriteRule ^$ ? [L,R=301]
但这会删除所有实例中的所有参数
也试过这个:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} (.*)?&?site=[^&]+&?(.*)? [NC]
RewriteCond %{REQUEST_URI} /(.*)
RewriteRule .* %3?%1%2? [R=301,L]
但这没有任何作用。发现很难理解这些重写规则。如果有人可以提供帮助,我将不胜感激
最佳答案
假设您的参数有时是 product
并且始终是 retailer
和 site
(按此顺序),您的规则应该如下所示
# first condition
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{QUERY_STRING} ^product=([^&\s]+)&retailer=([^&\s]+)&site=test$ [NC]
RewriteRule ^$ /?product=%1&retailer=%2 [R=301,L]
# second condition
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{QUERY_STRING} ^retailer=([^&\s]+)&site=test$ [NC]
RewriteRule ^$ /retailer/%1? [R=301,L]
关于apache - 如果某个 GET 参数不存在,则仅从 url 中删除一个 GET 参数并重写另一个 - htaccess,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44130406/