.htaccess mod_rewrite – 如何从重写规则中排除目录

我在.htaccess文件中有8行重写规则。 我需要从这些规则中排除服务器上的两个物理目录,以便它们可以被访问。 现在所有请求都发送到index.php文件。

目录排除:“pipe理员”和“用户”。

所以http请求:http: //www.domain.com/admin/不应该传递给index.php文件。

ErrorDocument 404 /index.php?mod=error404 Options FollowSymLinks RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC] RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L] RewriteRule ^([^/] )/([^/] )\.html$ index.php?lang=$1&mod=$2 [L] RewriteRule ^([^/] )/$ index.php?lang=$1&mod=home [L] 

在你的其他规则之前试用这个规则:

 RewriteRule ^(admin|user)($|/) - [L] 

这将结束重写过程。

你也可以做的是把一个.htaccess文件包含

 RewriteEngine Off 

在您想要排除的文件夹中被重写(通过树中较高的.htaccess文件中的规则)。 简单而有效。

添加一个条件来检查pipe理目录,如下所示:

 RewriteCond %{REQUEST_URI} !^/?(admin|user)/ RewriteRule ^([^/] )/([^/] )\.html$ index.php?lang=$1&mod=$2 [L] RewriteCond %{REQUEST_URI} !^/?(admin|user)/ RewriteRule ^([^/] )/$ index.php?lang=$1&mod=home [L] 
 RewriteEngine On RewriteRule ^(wordpress)($|/) - [L] 

如果你想从规则中删除一个特定的目录(也就是说,你想删除目录foo ),你可以使用:

 RewriteEngine on RewriteCond %{REQUEST_URI} !^/foo/$ RewriteRule !index\.php$ /index.php [L] 

上面的rewriteRule将把所有的请求重写到/index.php,不包括/ foo /的请求。

要排除所有现有的直播,您需要在规则上方使用以下条件:

 RewriteCond %{REQUEST_FILENAME} !-d 

以下规则

 RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteRule !index\.php$ /index.php [L] 

将所有内容(directries除外)重写为/index.php

我们使用了下面的mod_rewrite规则:

重写引擎
RewriteCond%{REQUEST_URI}!^ / test /
RewriteCond%{REQUEST_URI}!^ / my-folder /
RewriteRule(。*) http://www.newdomain.com/ $ 1 [R = 301,L]

除了对/ test和/ my-folder目录中的资源的请求之外,这会将所有到站点的stream量redirect(永久为301redirect)到http://www.newdomain.com 。 我们通过使用(。*)捕获组将用户转移到他们请求的确切资源,然后在新的URL中包含$ 1。 注意空间。