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

时间:2009-12-04 17:34:29

标签: .htaccess mod-rewrite url-rewriting

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

要排除的目录:“admin”和“user”。

所以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]

6 个答案:

答案 0 :(得分:262)

在您的其他规则之前尝试此规则:

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

这将结束重写过程。

答案 1 :(得分:97)

你还可以做一个包含

的.htaccess文件
RewriteEngine Off

在要重写的文件夹中(通过树中较高的.htaccess文件中的规则)。简单但有效。

答案 2 :(得分:22)

添加条件以检查管理目录,例如:

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]

答案 3 :(得分:4)

RewriteEngine On

RewriteRule ^(wordpress)($|/) - [L]

答案 4 :(得分:4)

我们使用了以下mod_rewrite规则:

  

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

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

答案 5 :(得分:3)

如果要从规则中删除特定目录(意味着,您要删除目录 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]

将所有内容(除了直接)重写为 /index.php

相关问题