代码之家  ›  专栏  ›  技术社区  ›  deceze

.htaccess指令指向*not*重定向某些URL

  •  21
  • deceze  · 技术社区  · 16 年前

    在一个严重依赖 .htaccess RewriteRules的prettyURL(在我的例子中是cakephp),如何正确地设置指令以从重写中排除某些目录?即:

    /appRoot/.htaccess
             app/
             static/
    

    默认情况下,每个请求 /appRoot/* 正在重写以供 app/webroot/index.php ,分析它并调用相应的控制器操作。这是由以下指令完成的: HTAccess :

    RewriteBase /appRoot
    
    RewriteRule ^$ app/webroot/     [L]
    RewriteRule (.*) app/webroot/$1 [L]
    

    我现在想从这个重写中排除一些目录,比如static/目录。我试过这个 之前 蛋糕重写:

    RewriteCond $1 ^(static|otherDir).*$ [NC]
    RewriteRule (.*) - [L]
    

    到目前为止,请求已不再被重写,但现在 全部的 请求被跳过,即使合法的蛋糕请求也不应匹配 ^(static|otherDir).*$ .

    我尝试了几种不同的规则,但不能让它按我想要的方式工作。

    3 回复  |  直到 12 年前
        1
  •  6
  •   Rais Alam    12 年前

    正确答案是…

    RewriteRule   ^(a|bunch|of|old|directories).* - [NC,L]
    
    # all other requests will be forwarded to Cake
    RewriteRule   ^$   app/webroot/   [L]
    RewriteRule   (.*) app/webroot/$1 [L]
    

    我仍然不明白为什么根目录中的index.php文件最初被调用,即使这些指令已经就位。它现在位于

    /appRoot/app/views/pages/home.ctp
    

    也可以通过蛋糕来处理。如果现在有了这个,我想这也会起作用(稍微修改一下Mike的建议版本,未经测试):

    RewriteCond $1      !^(a|bunch|of|old|directories).*$ [NC]
    RewriteRule ^(.*)$  app/webroot/$1 [L]
    
        2
  •  1
  •   Alasdair    16 年前

    您是否不能将条件应用于以下规则,但使用否定,如中所述(其中有一些变化,我不太擅长记住.htaccess规则,因此标记可能是错误的):

    RewriteCond $1 !^(static|otherDir).*$ [NC]
    RewriteRule ^$ app/webroot/ [L]
    
    RewriteCond $1 !^(static|otherDir).*$ [NC]
    RewriteRule ^$ app/webroot/$1 [L]
    
        3
  •  1
  •   Jeff Atwood    16 年前

    从以前的规则中删除[l]。

    RewriteBase /appRoot
    
    RewriteRule ^$ app/webroot/    
    RewriteRule (.*) app/webroot/$1
    

    [L]的意思是“停止这里的重写过程,不要再应用任何重写规则。”