2

要么我真的需要回到课桌,要么发生了一些奇怪的事情。

以下不起作用,因为实际的物理文件和目录无法解析:

<IfModule mod_rewrite.c>
  Options +FollowSymLinks
  RewriteEngine on
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-l

  # The following rule must not affect real physical files, but does
  RewriteRule ^(img/.*)$ http://old.site.com/$1 [L,R=301]

  RewriteRule .* index.php [L]
</IfModule>

但是,这段代码可以正常工作并解析真实的文件和文件夹:

<IfModule mod_rewrite.c>
  Options +FollowSymLinks
  RewriteEngine on
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-l

  RewriteRule ^(img/.*)$ http://old.site.com/$1 [L,R=301]

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-l

  RewriteRule .* index.php [L]
</IfModule>

我真的需要在每个 RewriteRule 之前都有一个新的 RewriteCond 吗?

4

2 回答 2

6

RewriteCond 仅适用于下一个 RewriteRule。所以是的,在你的情况下,你需要一个 RewriteCond 在每个 RewriteRule 之前。

但好消息是它可以避免。

如果您想避免编写这些多个 RewriteCond ,您可以使用以下代码:

## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]

RewriteRule ^(img/.*)$ http://old.site.com/$1 [L,R=301]

RewriteRule .* index.php [L]
于 2013-07-23T09:46:53.563 回答
3

是的。

你不能有两个RewriteRule基于一个(组)RewriteConds。

手册中的一些引用:

Each rule can have an unlimited number of attached rule conditions...

One or more RewriteCond can precede a RewriteRule directive...

The RewriteRule directive [...] can occur more than once, with each instance defining a single rewrite rule...

于 2013-07-23T09:45:45.390 回答