代码之家  ›  专栏  ›  技术社区  ›  Sandeepan Nath

条件重写规则-如何仅应用这两组规则之一

  •  1
  • Sandeepan Nath  · 技术社区  · 15 年前

    如果有 browse_by_exam my_study_centre 在第一个位置,从 project_name 如在

    http://project_name/browse_by_exam/type/classes/...
    http://project_name/my_study_centre/page_sort/1,20,name,asc/...
    

    然后适用这两条规则:

    RewriteRule ^([a-zA-Z0-9\-\_]+)/?$ index.php?a=$1 
    RewriteRule ^([a-z0-9\-\_]+)/([a-z0-9\-\_]+)/?$ index.php?a=$1&b=$2
    

    对于所有其他URL,应用以下规则:

    RewriteCond $1 !^(index\.php|resources|robots\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php [L,QSA]
    

    谢谢,

    桑迪桑

    2 回复  |  直到 15 年前
        1
  •  1
  •   Sandeepan Nath    15 年前

    我自己找到了解决办法。以下工作:

    ############Following rules apply only to browse_by_exam and my_study_centre
    
    RewriteCond $1 !^(index\.php|resources|robots\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond $1 ^(browse_by_exam|my_study_centre) 
    
    RewriteRule ^(.*)$ index.php [L,QSA]
    #################################################
    
    
    ###Following rules apply to all other pages (other than browse_by_exam and my_study_centre)
    RewriteRule ^([a-zA-Z0-9\-\_]+)/?$ index.php?a=$1 
    
    RewriteRule ^([a-z0-9\-\_]+)/([a-z0-9\-\_]+)/?$ index.php?a=$1&b=$2
    
    RewriteRule ^([a-z0-9\-\_]+)/([a-z0-9\-\_]+)/([a-zA-Z0-9\-\_+]+)/?$ index.php?a=$1&b=$2&c=$3
    
    RewriteRule ^([a-z0-9\-\_]+)/([a-z0-9\-\_]+)/([a-zA-Z0-9\-\_+]+)/([0-9]+)/?$ index.php?a=$1&b=$2&c=$3&d=$4
    

    干杯,

    桑迪桑

        2
  •  0
  •   Gumbo    15 年前

    这些URL中没有一个与您的两个规则的模式匹配。但是,如果要编写符合条件的规则,请尝试以下规则:

    RewriteRule ^(browse_by_exam|my_study_centre)($|/) foobar [L]