2013-07-23 166 views
2

要么我真的需要回到学校的办公桌,要么有一些奇怪的事情发生。.htaccess重写问题

下不起作用,因为真正的物理文件和目录没有解决:

<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?

回答

6

的RewriteCond只适用于第二天重写规则。所以是的,在你的情况下,你需要在每个RewriteRule之前有一个RewriteCond。

但好消息是它可以避免。

如果你想避免写这些多重的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] 
+1

我非常感谢这个出色的答复和解决方法。 – tim

+0

非常欢迎您,很高兴您喜欢这种解决方法。 – anubhava

3

是的。

根据一套(一套)RewriteCond s不能有两个RewriteRule s。

manual的一些话:

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...

+0

* voteup *非常感谢您的回答。我会接受anubhavas答案,因为它提供了一个很好的解决方法示例。但我想分享我的感谢,指出这一点。 – tim