2014-09-03 68 views
1

我有我的htaccess文件設置像這樣:htaccess的服務器端WEBP檢測

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    # check if browser accepts webp 
    RewriteCond %{HTTP_ACCEPT} image/webp 

    # check if file is jpg or png 
    RewriteCond %{REQUEST_FILENAME} (.*)\.(jpe?g|png)$ 

    # check if corresponding webp file exists image.png -> image.webp 
    RewriteCond %1\.webp -f 

    # serve up webp instead 
    RewriteRule (.+)\.(jpe?g|png)$ $1.webp [T=image/webp,E=accept:1] 

</IfModule> 

<IfModule mod_headers.c> 
    Header append Vary Accept env=REDIRECT_accept 
</IfModule> 

AddType image/webp .webp 

其中在一個測試目錄的作品真的很好,表現出了WEBP圖像(如果存在)。但是,當我試圖將相同的.htaccess規則與WordPress的永久鏈接重寫規則結合使用時,規則就會中斷。

這是WordPress的默認規則(如果您的子目錄被稱爲DPA)

<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase /DPA/ 
RewriteRule ^index\.php$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . /DPA/index.php [L] 
</IfModule> 

我會在服務器出現以下錯誤,如果我試圖直接訪問JPG文件:

The requested URL /wp-content/themes/dpa/assets/img/home-page/sliders/strategy-1650.webp was not found on this server. 

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request. 

我想這是因爲WordPress在根目錄中查找文件並弄亂圖像文件的實際位置。

有誰知道在Apache上足夠好的重寫規則,以使兩個規則相互兼容?

謝謝!

帕特里克

+1

你把你的規則放在WordPress的上面還是下面?我通常將WordPress的規則保持在底部。另外,你有沒有嘗試在你的重寫中加入「最後的規則」標誌'L'? '[T = image/webp,E = accept:1,L]' – 2014-09-03 13:15:33

+0

我嘗試了上面和下面,但它似乎沒有什麼區別。我現在嘗試一下L標誌 – patrickzdb 2014-09-03 13:17:16

+0

試試上面和'L',這樣它會首先啓動,而WordPress根本不會涉及。 – 2014-09-03 13:17:54

回答

4

這爲我工作,

<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase /DPA/ 
RewriteRule ^index\.php$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . /DPA/index.php [L] 
</IfModule> 

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteBase /DPA/ 
    # check if browser accepts webp 
    RewriteCond %{HTTP_ACCEPT} image/webp 

    # check if file is jpg or png 
    RewriteCond %{REQUEST_FILENAME} (.*)\.(jpe?g|png)$ 

    # check if corresponding webp file exists image.png -> image.webp 
    RewriteCond %1\.webp -f 

    # serve up webp instead 
    RewriteRule (.+)\.(jpe?g|png)$ $1.webp [T=image/webp,E=accept:1] 

</IfModule> 

<IfModule mod_headers.c> 
    Header append Vary Accept env=REDIRECT_accept 
</IfModule> 

AddType image/webp .webp 

我加RewriteBase/DPA /到WEBP檢查,現在它工作正常。

我剛剛意識到我包含了默認的WordPress漂亮永久鏈接代碼,但它應該是目錄中的一個。我現在將編輯該問題。