2014-02-25 27 views
0

我的問題很簡單:如何檢查重寫URL時mod_rewrite花費的時間?

是否有可能知道apache mod_rewrite重寫url的時間?

我的網站有大約2000萬個請求/月。

在這種情況下,我想知道在這些規則中花費的時間。

<IfModule mod_rewrite.c> 

    RewriteEngine On 
    RewriteBase/

    # Remove Multiple slashes in betweeen 
    RewriteCond %{REQUEST_URI} ^(.*)//(.*)$ 
    RewriteRule . %1/%2 [R=301,L] 

    # Send everything to index.php 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(.*)$ some_path/index.php/$1 [L] 

</IfModule> 



預先感謝

回答

1

如果可能,請將重寫移動到VirtualHost配置,因此不會爲.htaccess中的每個請求編譯正則表達式。 例如,對於Apache的,在httpd.conf中把你的.htaccess的內容是這樣的:

<Directory /www/htdocs/example> 
    <IfModule mod_rewrite.c> 

     RewriteEngine On 
     RewriteBase/

     # Remove Multiple slashes in betweeen 
     RewriteCond %{REQUEST_URI} ^(.*)//(.*)$ 
     RewriteRule . %1/%2 [R=301,L] 

     # Send everything to index.php 
     RewriteCond %{REQUEST_FILENAME} !-f 
     RewriteCond %{REQUEST_FILENAME} !-d 
     RewriteRule ^(.*)$ some_path/index.php/$1 [L] 

    </IfModule> 
</Directory> 
+0

謝謝,我的規則不在項目根目錄中,我將它放在虛擬主機配置中,以防止在每個請求中解析文件(.htaccess) –

+0

您將.htaccess設置爲關鍵字,所以我以爲你做到了。 – Revolution88

+0

對不起,我把這個關鍵字的更大的知名度。 –

1

你可以檢查螢火蟲或其他瀏覽器工具的「等待」的時間來檢查多少時間花在請求之間和瀏覽器實際接收數據。

重定向到靜態資源當然會更好,以避免評估PHP處理請求的時間。

對於coure,它仍然會包含Apache通常用來處理請求的時間,並且您可能想要測試兩種情況:使用重定向和不使用。

順便說一句,如果你正在尋找優化這些,你可以嘗試更換此:

RewriteCond %{REQUEST_URI} ^(.*)//(.*)$ 
RewriteRule . %1/%2 [R=301,L] 

有了這個:

RewriteRule ^(.*)//(.*)$ $1/$2 [R=301,L] 

有沒有真正的需要單獨使用這種的RewriteCond的,更復雜的重寫規則就足夠了。

此外,

RewriteRule ^(.*)$ some_path/index.php/$1 [L] 

可與

RewriteRule (.*) some_path/index.php/$1 [L] 

起始與結束字符不會被髮送$ 1,因此它可以被包括在規則中,因此簡化它來代替。

+0

非常感謝你,但在服務器級別就沒有辦法檢查嗎? –

+0

您可以激活一些重寫日誌,但它可能會減慢請求速度,正如文檔中提到的:http://httpd.apache.org/docs/current/mod/mod_rewrite.html#logging。因此,您無法清楚地瞭解在這些設備上花了多少時間。 – Capsule

+0

是的,日誌不是一個好的方法,但我認爲要測試這個,我將執行大約1000個相同的請求到一個沒有代碼的靜態頁面,並計算花費的時間。 –