Php htaccess正则表达式-不遵循模式

Php htaccess正则表达式-不遵循模式,php,regex,.htaccess,Php,Regex,.htaccess,我对底部的正则表达式有问题。我试图通过索引文件传递所有模式,除了index.php、robots.txt和任何css和js文件,但是我有一个特定的路径,需要通过php处理js和css文件。通常我的url结构如下所示: example.com/class/function 假设我想从以下模式处理css和js文件: example.com/view/file/xxxxx.js/css 通常我会认为这段代码会起作用,但它不会: (.*)(?!view\/file)(.*)\.(js|css) 出于一

我对底部的正则表达式有问题。我试图通过索引文件传递所有模式,除了index.php、robots.txt和任何css和js文件,但是我有一个特定的路径,需要通过php处理js和css文件。通常我的url结构如下所示:

example.com/class/function

假设我想从以下模式处理css和js文件:

example.com/view/file/xxxxx.js/css

通常我会认为这段代码会起作用,但它不会:

(.*)(?!view\/file)(.*)\.(js|css)
出于一些奇怪的原因,此代码可以工作(删除了“视图”并添加了/在(.*)之前):

但是我将无法从主目录加载文件:

example.com/file.js

当我像这样修改文件时,它就不再工作了。。。(我所做的只是做/可选):

以下是原始htaccess文件:

RewriteCond $1 !^(index\.php|robots\.txt|(.*)\.(js|css)) 
RewriteRule ^(.*)$ /index.php/$1 [L]

假设我理解正确,这样的方法应该会奏效:

#rewrite specific css/js
RewriteRule ^view/file/(.*)\.(css|js)$ file.php?file=$1.$2 [L]

#only rewrite anything else if the path is not an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#rewrite to main handler
RewriteRule ^(.*)$ index.php/$1 [L]

为了可读性和可调试性,我建议您将一些规则分开

# Rewrite the special case
RewriteRule ^view/file/(rest of rule)

# Skip these
RewriteRule ^index\.php$ - [L]
RewriteRule ^robots\.txt$ - [L]
RewriteRule ^(.*)\.(js|css)$ - [L]

# Main rule
RewriteRule ^(.*)$ /index.php/$1 [L]

我没有想过要把它们分开,但是如果我想把它们结合起来,(?!view/file)难道不应该起作用吗?丹尼斯,我认为汤姆·黑格的想法(和我的想法)省去了消极的前瞻,因为它令人困惑,很难判断它是否“应该”起作用。在mod_rewrite中,有时更容易定义您想要匹配的内容和应该重写的内容。
#rewrite specific css/js
RewriteRule ^view/file/(.*)\.(css|js)$ file.php?file=$1.$2 [L]

#only rewrite anything else if the path is not an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#rewrite to main handler
RewriteRule ^(.*)$ index.php/$1 [L]
# Rewrite the special case
RewriteRule ^view/file/(rest of rule)

# Skip these
RewriteRule ^index\.php$ - [L]
RewriteRule ^robots\.txt$ - [L]
RewriteRule ^(.*)\.(js|css)$ - [L]

# Main rule
RewriteRule ^(.*)$ /index.php/$1 [L]