htaccess RewriteCond,RewriteRule的多重条件

时间:2011-10-24 16:58:44

标签: .htaccess mod-rewrite

我使用.htaccess Mod Rewrite将URL发送到index.php,然后它会解析URL并构建我网站的每个页面。 这非常合适,允许我使用PHP而不是.htaccess轻松管理干净的URL。

例如:http://domain.com/some/url/here/ - >转到index.php,它会加载一个包含多个PHP文件的页面

但是,我试图允许某些PHP文件作为常规PHP文件加载(不将URL发送到index.php进行解析)。

例如:http://domain.com/some/url/here/仍然如上所述。 http://domain.com/specialexception.php将作为常规php文件加载,而不发送到index.php

我有以下代码:

<IfModule mod_rewrite.c>

    RewriteEngine On

    RewriteCond $1 !^specialexception\.php$ [NC]
    RewriteRule !\.(css|gif|jpg|png|ico|txt|xml|js|pdf|htm|zip)$ /path/to/index.php [NC,L]

</IfModule>

然而,RewriteCond系列现在被忽略了。

我将不胜感激任何帮助/想法!

谢谢。

2 个答案:

答案 0 :(得分:2)

我想通了,虽然我不确定为什么会这样:

我改变了规则,所以它们以相反的顺序发生。 有效的代码如下:

RewriteCond %{REQUEST_URI} !\.(css|gif|jpg|png|ico|txt|xml|js|pdf|htm|zip)$
RewriteRule !(specialexception)+\.php)$ /path/to/index.php [NC,L]

此解决方案的问题: 由于“specialexception.php”被列为最后一条规则,在实际的RewriteRule行中,它只解决了一个文件异常的问题。

围绕这个的一个混乱的方法可能是在正则表达式的最后一行使用管道:

RewriteCond %{REQUEST_URI} !\.(css|gif|jpg|png|ico|txt|xml|js|pdf|htm|zip)$
RewriteRule !(specialexception|anotherexception|foo|bar)+\.php)$ /path/to/index.php [NC,L]

如果有人对如何为多个文件添加多个例外有更好的了解,我很想知道!

答案 1 :(得分:2)

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !(specialexception)\.php$
    RewriteCond %{REQUEST_URI} !(anotherexception)\.php$
    RewriteRule !\.(css|gif|jpg|png|ico|txt|xml|js|pdf|htm|zip)$ index.php [NC,L]
</IfModule>

这对我来说很好。如果.htaccess位于index.php所在的目录中,请不要使用webroot中的路径。而是使用相对于.htaccess文件的路径。

相关问题