如何排除除一个目录RewriteCond之外的所有文件和目录

时间:2012-09-19 21:06:09

标签: apache .htaccess mod-rewrite

我想将所有非文件,非目录重定向到index.php,因此存在的所有内容都可以直接访问 - 除了一个目录(也应该转到index.php)

我拥有的是:

# Files
RewriteCond %{REQUEST_FILENAME} !-f

# Directories except /orderfiles/*
RewriteCond %{REQUEST_FILENAME} ^/(orderfiles/)* [OR]
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule .* index.php [L,QSA]

这适用于/ orderfiles中的目录,但/ orderfiles中的文件仍然会转到该文件。我尝试添加

RewriteCond %{REQUEST_FILENAME} ^/(orderfiles/)* [OR]
RewriteCond %{REQUEST_FILENAME} !-f

但是这会中断并将每个文件发送到index.php。

1 个答案:

答案 0 :(得分:4)

你很接近,你想要这样的东西:

# Files
RewriteCond %{REQUEST_FILENAME} !-f

# Directories
RewriteCond %{REQUEST_FILENAME} !-d

# except /orderfiles
RewriteRule !^/?orderfiles index.php [L,QSA]

  

我意识到我错误地输入了我想要做的事情,我的意思是“但是将一个目录发送到index.php”(我编辑了我的问题以便更准确)。我确实希望/ orderfiles被路由到index.php

那么你所拥有的是正确的,但你需要改变这一行:

RewriteCond %{REQUEST_FILENAME} ^/(orderfiles/)* [OR]

为:

RewriteCond %{REQUEST_URI} ^/orderfiles/ [OR]
相关问题