2013-07-23 97 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