2011-03-16 49 views
2

有人可以解釋下面的htacess行,我理解部分,但是想要更深入的知識。作爲一個說明,我假設它按預期工作,這不是現場直播,我只是通過閱讀一些工作簿,這是打印。解釋這個.htaccess片段

// Don't understand this line 
Options -Multiviews 

// Don't understand this line 
Options +FollowSymLinks 

// Understand this line 
RewriteEngine On 

// Don't ~fully~ understand this line, esp. in context 
RewriteBase /portfolio 

// Don't ~fully~ understand this line 
// I understand that its asking if the filename is a valid file or dir 
// but is it overall saying if valid file or valid dir perform rewrite? 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

// Don't understand, $1 is the STRING, and the rest the condition, but 
// how do you read this condition? 
RewriteCond $1 !^(index\.php|images|robots\.txt|css) 

// Don't understand (but do understand the RewriteRule PATTERN REPLACE, but is it 
// saying replace 'all' with index.php/all ? 
RewriteRule ^(.*)$ index.php?/$1 

回答

5
Options -Multiviews 

這將禁用在Multiviews Apache的選項。基本上,該選項允許服務器根據客戶端接受的內容類型和語言,使用不同的文件名查找同一目錄中的內容。在這種情況下,該指令僅被禁用,以確保Apache不會提供任何意外的文件。

在Multiviews使內容協商,這是在解釋說:http://httpd.apache.org/docs/current/content-negotiation.html

Options +FollowSymLinks 

這可以確保啓用了FollowSymLinks選項。該設置允許Apache在目錄中存在符號文件鏈接。如果存在符號文件鏈接以使文件物理地存在於服務器上的其他位置而不是所請求的位置,則該設置存在。在

較長的解釋:http://www.maxi-pedia.com/FollowSymLinks

RewriteBase /portfolio 

該設置用於限定用於通過重寫引擎使用url的基本路徑。當重寫引擎在.htaccess中重寫URL時,它將去掉當前目錄的路徑。一旦URL重寫完成後,它會根據當前文件目錄將其添加回去。但是,有時請求的url與服務器本身的目錄結構路徑不同。 RewriteBase告訴重寫引擎URL路徑到當前目錄。例如,在這種情況下,這些文件可能存儲在/foo/bar中,但它們可以通過瀏覽器作爲www.example.com/portfolio訪問。 RewriteBase告訴引擎將/portfolio添加到url,而不是/foo/bar

有關完整的說明,請參閱:http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase(該URL還包含對.htaccess的其他重寫部分的解釋)。

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

這些行確保任何實際存在的文件或目錄的url都不會被重寫。條件爲否定之前的!。所以這兩個條件應該被解讀爲ifNotFile AND ifNotDirectory

RewriteCond $1 !^(index\.php|images|robots\.txt|css) 

$1這裏指的是實際重寫的子圖案捕獲1。換句話說,它表示在RewriteRule中由(.*)捕獲的部分。基本上,這個規則只是檢查RewriteRule將不會重寫任何以「index.php」,「images」,「robots.txt」或「css」開頭的url。

​​

這只是告訴任何請求(即不被重寫條件防止當然)應該被重寫爲index.php?與它後面的實際請求重寫引擎。就像你說的,foo/bar的請求將被轉發到index.php?foo/bar。關鍵是允許index.php處理文件請求(可以通過$_SERVER['QUERY_STRING']訪問它們),這在CMS系統和框架中非常常見。

我希望這些解釋會有所幫助。我對所有這些指令都沒有豐富的經驗,所以可能會存在一些細微的不準確之處,如果是這樣,請發表評論。

+0

很好的解釋 - 欣賞它 – Chris 2011-03-21 05:16:50