duanshan5259
2015-02-11 15:17
浏览 90
已采纳

htaccess页面重定向参数条件

How can I apply different RewriteRule in .htaccess depending on number of parameters in URL? For example,

1) If the URL has 1 parameter than it should go to products.php

 www.domain.com/mobile
 RewriteRule ^([0-9a-zA-Z_-]+) products.php?parent=$1 [NC,L]

2) If it has 2 parameters then it should go to category.php

 www.domain.com/mobile/apple
 RewriteRule ^([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+) category.php?parent=$1&child=$2 [NC,L]

3) If it has 3 parameters then it should go to list.php

 www.domain.com/mobile/apple/iphone6
 RewriteRule ^([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+) list.php?parent=$1&child=$2&subchild=$3 [NC,L]

I am a newbie in .htaccess but as you can see I have learnt to write RewriteRule but cannot understand that how can I put all of my above rules in one single .htaccess file because if I put it like this

RewriteEngine On
RewriteRule ^([0-9a-zA-Z_-]+) products.php?parent=$1 [NC,L]
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+) category.php?parent=$1&child=$2 [NC,L]
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+) list.php?parent=$1&child=$2&subchild=$3 [NC,L]

it gets confused and off-course it should be but how can apply these above rules depending on number of parameters? I hope I made my question clear enough.

I meant to say that if it was a programming question than something like this

if($countParameters = 1)
{
   //Rule 1
}
else if($countParameters = 2)
{
   //Rule 2
}
else if($countParameters = 3)
{
   //Rule 3
}

but its .htaccess and I cannot understand how can I apply my rules depending on number of parameters.

图片转代码服务由CSDN问答提供 功能建议

如何在 .htaccess 中应用不同的 RewriteRule 取决于 在URL中的参数数量? 例如,

1)如果URL有1个参数,那么它应该转到products.php

  www.domain.com/  mobile 
 RewriteRule ^([0-9a-zA-Z _-] +)products.php?parent = $ 1 [NC,L] 
   
 
 

2)如果 它有2个参数然后它应该转到category.php

  www.domain.com/mobile/apple
 RewriteRule ^([0-9a-zA-Z_-]  +)/([0-9a-zA-Z _-] +)category.php?parent = $ 1& child = $ 2 [NC,L] 
   
 
 

3)如果它有3个参数,则应该转到list.php

  www.domain.com/mobile/apple/iphone6
 RewriteRule ^([0-9a-  zA-Z _-] +)/([0-9a-zA-Z _-] +)/([0-9a-zA-Z _-] +)list.php?parent = $ 1& child = $ 2& subchild  = $ 3 [NC,L] 
   
 
 

我是 .htaccess 的新手但是你可以看到我学会了写 RewriteRule 但无法理解我怎样才能将上述所有规则放在一个.htaccess文件中,因为如果我把它放在这个

  RewriteEngine On 
RewriteRule  ^([0-9a-zA-Z _-] +)products.php?par  ent = $ 1 [NC,L] 
RewriteRule ^([0-9a-zA-Z _-] +)/([0-9a-zA-Z _-] +)category.php?parent = $ 1& child = $ 2  [NC,L] 
RewriteRule ^([0-9a-zA-Z _-] +)/([0-9a-zA-Z _-] +)/([0-9a-zA-Z _-] +)列表 .php?parent = $ 1& child = $ 2& subchild = $ 3 [NC,L] 
   
 
 

它变得混乱,当然它应该是但如何 可以根据参数的数量应用上述规则吗? 我希望我的问题足够清楚。

我的意思是说,如果这是一个编程问题而不是像这样的

  if  ($ countParameters = 1)
 {
 //规则1 
} 
如果($ countParameters = 2)
 {
 //规则2 
} 
如果($ countParameters = 3)
  {
 //规则3 
} 
   
 
 

但是 .htaccess 我无法理解如何应用我的规则取决于 参数数量。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dtotuki47568 2015-02-11 15:19
    已采纳

    Yes sure, make sure to use anchor $ in your regex to make sure it doesn't match more than intended.

    RewriteEngine On
    RewriteRule ^([\w-]+)/?$ products.php?parent=$1 [QSA,L]
    RewriteRule ^([\w-]+)/([\w-]+)/?$ category.php?parent=$1&child=$2 [QSA,L]
    RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ list.php?parent=$1&child=$2&subchild=$3 [QSA,L]
    
    • /?$ allows an optional trailing slash at the end of your URLs
    • Note I have used \w instead of [a-zA-Z0-9_]
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题