mod_rewrite 免除网址的语法

Syntax for mod_rewrite to exempt urls

告诉 mod_rewrite 停止处理 url 列表规则的最佳方法是什么?

通常你会这样做:

RewriteCond %{REQUEST_URL} !^/foo
RewriteRule a b [L]

只有当有更多规则时,您才需要确保它们不再适用于 ^/foo!

有什么好说的:

RewriteCond %{REQUEST_URL} ^/foo
#no rewrite rules apply ever

由于 RewriteRule 本身可以匹配 URI,因此您不需要使用 RewriteCond。一个 RewriteRule 的重写操作只是带有 [L] 标志的 - 连字符应该防止匹配路径被重写。

# First prevent /foo from rewriting later
# This rule matches /foo and takes *no action* 
RewriteRule ^foo - [L]

# Other rules may follow...
# For example a generic rewrite in common use
RewriteCond %{REQUEST_FILENAME] !-f
RewriteCond %{REQUEST_FILENAME] !-d
RewriteCond (.*) index.php?id= [L]

注意:我使用 ^foo 假设这将在 .htaccess 的上下文中使用。如果您在服务器级别使用它,则需要包括前导 /,如 ^/foo.