当2个参数有效时,重写.htaccess中的URL

时间:2014-07-30 19:26:53

标签: apache .htaccess mod-rewrite redirect url-rewriting

我有这样的网址:

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.phpproducts_id的链接不会重定向。

1 个答案:

答案 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

相关问题