1

我的 htaccess 文件设置如下:

<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 上重写规则足以使这两个规则相互兼容?

谢谢!

帕特里克

4

1 回答 1

6

这对我有用,

<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 漂亮的永久链接代码,但它应该是用于目录的代码。我现在将编辑问题。

于 2014-09-03T13:32:45.230 回答