我有这样的网址:
index.php?cPath=23
index.php?cPath=23&sort=4a&language=En
index.php?currency=EUR&cPath=23&sort=4a&language=nl
index.php?currency=HUF&cPath=23&sort=4a&language=Hu
我想将所有这些内容重定向到此链接:shop/food
。所以我把它放在我的htaccess文件中:
# 301 --- /index.php?*cPath=23* => /shop/food
RewriteCond %{QUERY_STRING} (^|&)cPath=23($|&)
RewriteRule ^index\.php$ /shop/food? [L,R=301]
这很有效。
但是......我也有一些这样的网址:
product_info.php?cPath=23&products_id=1324&language=En&osCsid=204giavieen8nmfv95b0dfrgs5
使用上述代码,此链接也会重定向到/shop/food
,因为cPath=23
位于链接中。
我正在寻找一个只在index.php在链接中时才重定向到/shop/food
的代码。这样,所有这些链接都将被重定向,而product_info.php?cPath=23
的链接将获得404未找到的错误。
或者换句话说,其中product_info.php
或products_id
的链接不会重定向。
答案 0 :(得分:1)
您可以使用此规则:
RewriteCond %{QUERY_STRING} (^|&)cPath=23($|&)
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^index\.php$ /shop/food? [L,R=301]
确保它位于RewriteEngine On
下方。
使用%{THE_REQUEST}: THE_REQUEST
变量表示Apache从您的浏览器收到的原始请求,并且在执行某些重写规则后不会被覆盖。此变量的示例值为GET /index.php?id=123 HTTP/1.1