2013-03-27 175 views
0

我有一个htaccess的,看起来像这样htaccess的重写文件夹

RewriteEngine On 
RewriteCond %{REQUEST_URI} !static/(.*)\. 
RewriteRule ^(.*)$ index.php?controller=$1 [QSA] 

它工作正常。 /静态文件夹请求保持不变,而其他文件则执行index.php文件。 但现在我必须添加另一个规则。当用户导航到/ action/something时,应该执行/actions/something.php。但是,当我添加以下行

RewriteRule ^action/(.*)$ actions/$1.php [QSA] 

它将请求中断到静态文件夹。

回答

1

没有理由,为什么它应该打破static,除非您在RewriteCond之后立即写下新规则。然而,你应该做的,重写到一个绝对的URL

RewriteEngine On 
RewriteCond %{REQUEST_URI} !static/(.*)\. 
RewriteRule ^(.*)$ /index.php?controller=$1 [QSA] 
RewriteRule ^action/(.*)$ /actions/$1.php 

RewriteCond看起来很不寻常。除非是有原因的改写静态页面没有点.,你应该减少RewriteCond只是

RewriteCond %{REQUEST_URI} !static/ 

更新

为了防止无限重写,你必须添加另一排除条件

RewriteCond %{REQUEST_URI} !^/index\.php$ 

action和必须排除以及

RewriteCond %{REQUEST_URI} !/actions?/ 

全部放在一起给

RewriteEngine On 
RewriteCond %{REQUEST_URI} !/static/ 
RewriteCond %{REQUEST_URI} !/actions?/ 
RewriteCond %{REQUEST_URI} !^/index\.php$ 
RewriteRule ^(.*)$ /index.php?controller=$1 [QSA] 
RewriteRule ^action/(.*)$ /actions/$1.php 
+0

完美,谢谢!尽管你可能有一个错误,因为控制器行应该是RewriteRule^/(。*)$ /index.php?controller=$1 [QSA](注意正则表达式中的正斜杠)。否则它会导致无限重定向,因为错误日志状态 – 2013-03-28 04:17:44

+0

@VladimirHraban不,前缀斜杠可防止循环,但也会停止调用控制器。我更新了答案。 – 2013-03-28 08:31:45