2015-06-14 36 views
2

我有一個包含以下內容的的.htaccess:的.htaccess - 防止無限循環使用URL重寫

<Files .htaccess> 
order allow,deny 
deny from all 
</Files> 

Options +FollowSymLinks 
RewriteEngine On 
RewriteCond %{REQUEST_URI} !^(.*)/maintenance/(.*)$ [NC] 

RewriteRule ^(.*).jpg$ /mysite/maintenance/transparent.png [NC,R=302,L] 
RewriteRule ^(.*).jpeg$ /mysite/maintenance/transparent.png [NC,R=302,L] 
RewriteRule ^(.*).gif$ /mysite/maintenance/transparent.png [NC,R=302,L] 
RewriteRule ^(.*).png$ /mysite/maintenance/transparent.png [NC,R=302,L] 

RewriteRule ^(.*).php$ /mysite/maintenance/maintenance.php [NC,R=302,L] 

測試本地主機上。

通過這些設置,我有當試圖加載http://localhost/mysite/test.php,(正確地)重定向到http://localhost/mysite/maintenance/maintenance.php

循環似乎是由於4圖像重定向(注無限循環:維護頁面有一個一位於維護文件夾根目錄中的唯一jpg背景圖像)。評論這4條重定向線路解決了這個問題。

但是我不明白爲什麼我進入死循環,因爲/ maintenance/path本身被排除在RewriteCond的重定向之外,爲什麼圖像上的重定向會干擾這個問題。

你能幫忙嗎?

回答

2

你必須重新排序RewriteConds這樣的:後

RewriteEngine On 

RewriteRule \.jpg$ /mysite/maintenance/transparent.png [NC,R=302,L] 
RewriteRule \.jpeg$ /mysite/maintenance/transparent.png [NC,R=302,L] 
RewriteRule \.gif$ /mysite/maintenance/transparent.png [NC,R=302,L] 
RewriteCond %{REQUEST_URI} !^.*/maintenance/.*$ [NC] 
RewriteRule \.png$ /mysite/maintenance/transparent.png [NC,R=302,L] 

RewriteCond %{REQUEST_URI} !^.*/maintenance/.*$ [NC] 
RewriteRule \.php$ /mysite/maintenance/maintenance.php [NC,R=302,L] 

RewriteCond指令只有第一個RewriteRule影響。

+0

有沒有辦法來組的RewriteRules一個獨特的(或組合)的RewriteCond下? – Oliver

+0

不幸的是,不...它是'mod_rewrite' [行爲](http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritecond) – umka

0

我知道這個答案已被接受,但這裏可以使用更短的方法:

RewriteEngine On 

RewriteRule \.(jpe?g|gif)$ /mysite/maintenance/transparent.png [NC,R=302,L] 

RewriteCond %{REQUEST_URI} !/maintenance/ [NC] 
RewriteRule \.(png|php)$ /mysite/maintenance/transparent.$1 [NC,R=302,L]