.htaccess htaccess重写文件夹

.htaccess htaccess重写文件夹,.htaccess,mod-rewrite,.htaccess,Mod Rewrite,我有一个.htaccess,看起来像这样 RewriteEngine On RewriteCond %{REQUEST_URI} !static/(.*)\. RewriteRule ^(.*)$ index.php?controller=$1 [QSA] 它很好用/静态文件夹请求保持不变,而其他请求执行index.php文件。 但现在我必须添加另一条规则。当用户导航到/action/something时,应该执行/actions/something.php。但当我添加以下行时 Rewrite

我有一个.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]

它会中断对静态文件夹的请求。

没有理由,为什么它会中断
静态
,除非您在
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$
也必须排除
操作

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

太好了,谢谢!虽然您可能有输入错误,但控制器行应该是RewriteRule^/(.*)$/index.php?controller=$1[QSA](注意正则表达式中的正斜杠)。否则会导致无限重定向,如错误日志states@VladimirHraban不,在斜杠前面加前缀可以防止循环,但也会停止调用控制器。我更新了答案。