第二个Rewriterule无法正常工作

时间:2013-10-02 02:26:22

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

我想在这里做两件事......

  1. http://site.com.au/brand.php?pBrand=THENORTHFACE重定向到http://site.com.au/brand/the-north-face(此工作正常)
  2. http://site.com.au/listingbrand.php?pBrand=THENORTHFACE重定向到http://site.com.au/brand/the-north-face这不起作用,重定向listingbrand.php?pBrand = DOSH或pBrand = ATKM时,它们都指向第一次重写的 - 北 - 面)。
  3. 如何为每个品牌进行第二次重写?另外,重复每个品牌的重写是否正确?

    RewriteCond %{QUERY_STRING} ^pBrand=THENORTHFACE$ [NC]
    RewriteRule ^brand\.php$ /brand/the-north-face/? [R=301,L]
    RewriteRule ^listingbrand\.php$ /brand/the-north-face/ [R=301,L]
    
    RewriteCond %{QUERY_STRING} ^pBrand=DOSH$ [NC]
    RewriteRule ^brand\.php$ /brand/dosh/? [R=301,L]
    RewriteRule ^listingbrand\.php$ /brand/dosh/ [R=301,L]
    
    RewriteCond %{QUERY_STRING} ^pBrand=ATKM$ [NC]
    RewriteRule ^brand\.php$ /brand/all-the-kings-men/? [R=301,L]
    RewriteRule ^listingbrand\.php$ /brand/all-the-kings-men/ [R=301,L]
    

1 个答案:

答案 0 :(得分:1)

你有这个规则3次:

RewriteRule ^listingbrand\.php$ ...

未使用 RewriteCond,因为RewriteCond仅适用于下一个RewriteRule。实际上,您甚至不需要单独的规则,因为之前的RewriteRule可以使用正则表达式中的OR处理brand.phplistingbrand.php

将您的代码更改为:

RewriteCond %{QUERY_STRING} ^pBrand=THENORTHFACE$ [NC]
RewriteRule ^(brand|listingbrand)\.php$ /brand/the-north-face/? [R=301,L,NC]

RewriteCond %{QUERY_STRING} ^pBrand=DOSH$ [NC]
RewriteRule ^(brand|listingbrand)\.php$ /brand/dosh/? [R=301,L,NC]

RewriteCond %{QUERY_STRING} ^pBrand=ATKM$ [NC]
RewriteRule ^(brand|listingbrand)\.php$ /brand/all-the-kings-men/? [R=301,L,NC]