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

时间:2008-08-06 08:15:28

标签: apache .htaccess mod-rewrite

在一个严重依赖.htaccess RewriteRules for 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请求。

我尝试了这些规则的几种变体,但却无法按照我想要的方式运行。

3 个答案:

答案 0 :(得分:6)

正确答案iiiiis ......

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]

答案 1 :(得分:1)

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

RewriteBase /appRoot

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

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

答案 2 :(得分:1)

您是否可以将条件应用于以下规则,但是否定,如(在某些变体中,我不太擅长记住.htaccess规则,因此标志可能是错误的):

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

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