.htaccess指令以不重​​定向某些URL

时间:2020-03-05 18:37:58  来源:igfitidea点击:

在一个严重依赖于.htaccessRewriteRules为其PrettyURLs的应用程序中(在我的情况下为CakePHP),如何正确设置指令以将某些目录从此重写中排除?那是:

/appRoot/.htaccess
         app/
         static/

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

RewriteBase /appRoot

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

我现在想从此重写中排除一些目录,例如static /。我在Cake RewriteRules之前尝试过此方法:

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

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

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

解决方案

回答

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

RewriteBase /appRoot

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

[L]的意思是"在这里停止重写过程,并且不再应用任何重写规则。"

回答

我们能否将条件不适用于以下规则,但要加上否定,例如:(有一些变化,我不太会记住.htaccess规则,因此标志可能是错误的):

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

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

回答

正确的答案

RewriteRule   ^(a|bunch|of|old|directories).* - [NC,L]

# all other requests will be forwarded to Cake
RewriteRule   ^$   app/webroot/   [L]
RewriteRule   (.*) app/webroot/ [L]

即使使用这些指令,我仍然不知道为什么最初会调用根目录中的index.php文件。现在位于

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

并通过Cake进行处理。现在有了这个,我想这也可以工作(对Mike的建议稍作改动,未经测试):

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