【问题标题】:.htaccess directives to *not* redirect certain URLs.htaccess 指令*不*重定向某些 URL
【发布时间】:2008-08-06 08:15:28
【问题描述】:

在一个严重依赖.htaccess RewriteRules 的PrettyURLs(在我的例子中是CakePHP)的应用程序中,我如何正确设置指令以从重写中排除某些目录?那就是:

/appRoot/.htaccess
         app/
         static/

默认情况下,对/appRoot/* 的每个请求都将被重写,以供app/webroot/index.php 接收,在那里对其进行分析并调用相应的控制器操作。这是由.htaccess 中的这些指令完成的:

RewriteBase /appRoot

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

我现在想从这次重写中排除一些目录,如 static/。我在Cake RewriteRules之前尝试过这个:

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

目前它的工作原理是不再重写请求,但现在所有请求都被跳过了,即使是不应该匹配^(static|otherDir).*$的合法Cake请求。

我尝试了这些规则的几种变体,但无法让它按我想要的方式工作。

【问题讨论】:

    标签: apache .htaccess mod-rewrite


    【解决方案1】:

    而正确的答案是……

    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
    

    也通过 Cake 处理。现在有了这个,我想这也会奏效(迈克建议的略微修改版本,未经测试):

    RewriteCond $1      !^(a|bunch|of|old|directories).*$ [NC]
    RewriteRule ^(.*)$  app/webroot/$1 [L]
    

    【讨论】:

      【解决方案2】:

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

      RewriteCond $1 !^(static|otherDir).*$ [NC]
      RewriteRule ^$ app/webroot/ [L]
      
      RewriteCond $1 !^(static|otherDir).*$ [NC]
      RewriteRule ^$ app/webroot/$1 [L]
      

      【讨论】:

        【解决方案3】:

        从前面的规则中删除[L]:

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

        [L] 表示“在此处停止重写过程,不再应用任何重写规则。”

        【讨论】:

          猜你喜欢
          • 2011-04-29
          • 2012-05-05
          • 2014-10-17
          • 1970-01-01
          • 2015-10-02
          • 2018-03-28
          • 1970-01-01
          • 2014-01-19
          • 1970-01-01
          相关资源
          最近更新 更多